• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer

GitopsCentral

  • Home
  • Courses
  • Roadmap
  • About
  • Log In
  • Sign Up

Composite types: Structs

April 13, 2019 by shaik zillani

gophers
©techort.com

Structs

Struct is a collection of fields.

Syntax

type User struct {
      username string
      email string
      phone string
}

var u1 User

Initializing Structs

Struct can be initialized using new keyword which will initialize fields with 0or in below manner,

 user1 := User {username: "sam", email: "sam123@gmail.com", phone: "987654322"}

Using new keyword,

user1 := new(User)

Example

package main

import "fmt"

type User struct {
        username string
        email string
        phone string
}

func main() {
  user1 := User {username: "sam", email: "sam123@gmail.com", phone: "987654322"}
  fmt.Println(user1)
}

Output

{sam sam123@gmail.com 987654322}

golang go,  golang,  structs

Primary Sidebar

en English
ar Arabiczh-CN Chinese (Simplified)nl Dutchen Englishfr Frenchde Germanhi Hindiid Indonesianit Italianja Japanesems Malaypt Portugueseru Russianes Spanishte Telugu

Course Tags

concurrency free go golang

Recent Posts

  • Roadmap to Mastery
  • Increase SSH connection timeout
  • Check Certificate expiry in Kubernetes
  • space invaders in golang
  • creating kubernetes cluster using eksctl
  • Facebook
  • GitHub
  • YouTube

Contact

Navigation

  • goacademy pro

Footer

  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter

info@gitopscentral.com


+91 8341443220

© Copyright 2016-2024 gitopscentral · All Rights Reserved ·