Go from Beginner to Expert: A Complete Guide to Learn Golang PART-18 Step-by-Step Guide to understand Structs in Golang.

Go from Beginner to Expert: A Complete Guide to Learn Golang PART-18

Step-by-Step Guide to understand Structs in Golang.

Md. Faiyaj Zaman
4 min readMar 15, 2023

--

Hey there, welcome back to our series on Go programming language. In this episode, we’re going to dive into one of the most essential data types in Go — structs! We’ll learn what they are, why they’re important, and how to use them in your code.

To know about Golang tools and frameworks in Golang, please check the previous article.

What is a struct in Go?

A struct is a composite data type that groups together zero or more values of any type. Think of a struct like a container that can hold different pieces of information — kind of like a class in object-oriented programming.

How to define a struct in Go:

In Go, you define a struct using the type keyword followed by the name of your struct and then a set of curly braces containing the fields of your struct. Here’s an example:

type Person struct {

Name string

Age int

}

Here we’ve defined a struct called Person with two fields: Name of type string, and Age of type int.

How to use a struct in Go:

To use a struct in Go, you first need to create an instance of the struct. You can do this using the var keyword followed by the name of your variable, and then assigning it a value using curly braces. Here’s an example:

var faiyaj Person

faiyaj.Name = "Faiyaj"

faiyaj.Age = 25

Here we’ve created a new variable called faiyaj of type Person, and assigned it a value with the Name “Faiyaj” and Age 25.

Accessing fields of a struct:

To access the fields of a struct, you use the dot . operator followed by the name of the field. Here’s an example:

fmt.Println(faiyaj.Name)

fmt.Println(faiyaj.Age)

Here we’re using the fmt.Println() function to print the values of faiyaj’s Name and Age fields.

Struct initialization:

You can also initialize a struct when you create it using the & operator and curly braces. Here’s an example:

zaman := &Person {

Name: "Zaman",

Age: 30,

}

Here we’ve created a new variable called zaman of type Person, and assigned it a value with the Name “Zaman” and Age 30 using the & operator and curly braces.

Embedded structs:

In Go, you can embed one struct into another struct using the type keyword followed by the name of the outer struct, followed by the struct keyword, and then the name of the inner struct. Here’s an example:

type Address struct {

City string

State string

}

type Person struct {

Name string

Age int

Address Address

}

Here we’ve defined two structs: Address and Person. We’ve then embedded the Address struct into the Person struct using the Address Address syntax.

Using pointers with structs:

In Go, you can use pointers with structs to modify the original values of the struct. Here’s an example:

func updateAge(person *Person, newAge int) {

person.Age = newAge

}

updateAge(&faiyaj, 30)

fmt.Println(faiyaj.Age) // Output: 30

Here we’ve defined a function called updateAge() that takes a pointer to a Person struct and an int value. We then update the Age field of the struct using the pointer. Finally, we call the updateAge() function with a pointer to faiyaj, and print out the new value of faiyaj’s

Customizing struct field names:

In Go, you can customize the field names of a struct by using tags. Tags are a way of adding metadata to struct fields. Here’s an example:

type Person struct {

Name string `json:"name"`

Age int `json:"age"`

}

Here we’ve added tags to the Name and Age fields of our Person struct. The json tag tells Go to encode and decode these fields in JSON format with the specified field names.

Anonymous structs:

In Go, you can create anonymous structs — structs that don’t have a name — using the struct{} syntax. Here’s an example:

func getPerson() struct {

Name string

Age int

} {
return struct {

Name string

Age int

} {

Name: "Muhammad",

Age: 40,

}
}

func main() {

person := getPerson()

fmt.Println(person.Name) // Output: Muhammad

}

Here we’ve defined a function called getPerson() that returns an anonymous struct with Name and Age fields. We then call getPerson() and assign the returned struct to a variable called person. Finally, we print out the value of person’s Name field.

Nested structs:

In Go, you can nest structs inside of other structs. This can be useful for organizing your code and data. Here’s an example:

type Address struct {

City string

State string

}

type Person struct {

Name string

Age int

Address struct {

City string

State string

}

}

faiyaj := Person {

Name: "Faiyaj",

Age: 25,

Address: struct {

City string

State string

}{

City: "Dhaka",

State: "Khilgaon",

},

}

fmt.Println(faiyaj.Address.City) // Output: Dhaka

Here we’ve defined two structs: Address and Person. We’ve then nested an anonymous Address struct inside of the Person struct. We create a new Person variable called faiyaj and assign it a value with a nested anonymous Address struct. Finally, we print out the value of faiyaj’s Address field’s City field.

And that’s it for structs in Go! Structs are a powerful tool for organizing and managing data in your code. Hope this tutorial has been helpful in teaching you how to define, use, and customize structs in your own Go programs.

Please check out the next Step-by-Step Guide to understand File I/O and Error Handling in Golang.

Thanks for reading!

--

--