Learning Go — Maps & Structs
By: Prateek Gupta
Tags: Learn To Code, Struct, Maps, Golang, Software Development

Struct

A struct (short for “structure”) is a collection of data fields with declared data types. Go has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types. Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type.

If you have practised C in your past programming experiences, then struct is easy for you to understand.

type Employee struct {

id int

name string

dept []string

} func main() { // initialise a struct var

emp := Employee{

id : 123,

name : "prateek",

dept : []string {

"Technology",

"MFP",

"Delivery",

},

}

fmt.Println( "Employee : ", emp)

} Result

Employee : {123 prateek [Technology MFP Delivery]}

Observe :

struct definition is declared outside the func main() struct instance is declared inside the func main() During instantiation, fields are assigned a value. “Delivery”, and }, end with trailing commas. You will get error without the traling commas.

Different methods of instantiation

Positional Declaration — We can omit the field names while assigning the values during initialization. Go will automatically map the value with the corresponding field in the definition based on the order of the field declaration. I would not recommend this because if someday, the definition is added with a new field in between the pre declared field then the Go compiler will map the values in the new order of fields declared which would generate an error.

emp2 := Employee{

123,

"prateek",

[]string {

"Technology",

"MFP",

"Delivery",

},

}

NOTE : As was dicussed in this post, the struct definition and fields can be exposed outside the current package if the naming starts with a Capital letter.

Shorthand notation

This shorthand notation is anonymous struct. This is available in just the function where it is used and not at the package level.

doc := struct {

name string

}{name: "prateek"}

fmt.Println("Doctor : ", doc) Result

Doctor : {prateek}

Structs are ValueType

Structs unlike maps are value type and not reference type. When the value of a variable is assigned a struct variable value, the value gets copied. Changes to the new variable does not side effect the original struct.

anotherDoc := doc

anotherDoc.name = "gupta"

fmt.Println("doc Doctor : ", doc)

fmt.Println("anotherDoc Doctor : ", anotherDoc) Result

doc Doctor : {prateek}

anotherDoc Doctor : {gupta}

Accessing values of struct

The values of fields inside a struct can be accessed using the dot (.) operator.

fmt.Printf( "Employee id : %v and name : %v and departments : %v",

emp.id, emp.name, emp.dept) Result

Employee id : 123 and name : prateek and departments : [Technology MFP Delivery]

& (address of) operator

Just like arrays, & works similar in structs. The referenced struct gets modified if the referee updated the value.

refDoc := &doc

refDoc.name = "gupta"

fmt.Println("doc Doctor : ", doc)

fmt.Println("refDoc Doctor : ", refDoc) Result

doc Doctor : {gupta}

refDoc Doctor : &{gupta}

Nested structs

Go does not have inheritance. Go instead follows composition. Go does not follow IsA relation instead follows hasA relation. Nested structs is the way to achieve this.

type Person struct {

name string

age int

} type Profession struct {

Person

myProfession string

} func main() {

myProf := Profession{

Person : Person{

name : "prateek",

age : 27,

},

myProfession: "Engineer",

}

fmt.Println(myProf) yourProf := Profession{}

yourProf.name = "prateek"

yourProf.age = 27

yourProf.myProfession = "Engineer"

fmt.Println(yourProf)

} Result

{{prateek 27} Engineer}

{{prateek 27} Engineer}

Observe the nesting of struct Person inside struct Profession.

You can observe the variables myProf and yourProf. Both are different methods to initialize the structs.

Tags

Tags is a way to highlight validation rules in structs. You can tag rules to fields inside structs and use reflect library to print/return these mapped rules to fields.