How to Use Structs in GoLang: A Comprehensive Tutorial
structs in GoLang: How to use structs in GoLang to create complex data structures. This blog post discussed structs in GoLang, which are user-defined types used to group data and model real-world entities. It showed how to declare and create named and anonymous structs, access and modify structs fields, and use nested structs to create more complex data structures. Structs are the basis for creating methods and interfaces in GoLang. A struct is a user-defined type that represents a collection of fields. It can be used where it makes sense to group the data into a single unit rather than having each as separate values. Sure, here is a code snippet that shows how to declare and use a struct type Address: It also can be written without specifying the field name Nested structs are structs that have fields that are themselves structs. This allows us to create more complex data structures representing hierarchical or nested relationships. For example, we can create a nested struct to represent a person with an address with fields such as street, city, and state. To declare a nested struct, we can either define each struct type separately and use them as field types, or we can define anonymous structs inside others. To create a nested struct, we can use either syntax for creating named or anonymous structs, but we have to specify the values for each nested field using curly braces. To access or modify fields of a nested struct, we have to use the dot (.) operator multiple times to reach the desired field. For example, to declare and use a nested struct Employee with an Address field, we can write: In this blog post, we have learned about structs in GoLang, which are user-defined types that represent collections of fields. We have seen how to declare and create named, and anonymous structs using different syntaxes, access and modify structs fields using the dot (.) operator, and use nested structs to create more complex data structures. Structs are helpful for grouping data into a single unit and modeling real-world entities. They are also the basis for creating methods and interfaces in GoLang. For further reading on structs in GoLang, you can check out these links: Introduction
For instance, an employee has a firstName, lastName, and age. Grouping these three properties into a single struct named Employee makes sense. A struct can also contain fields of different types, such as string, int, float64, etc. This concept is generally compared with the classes in object-oriented programming, but structs are simpler and do not support inheritance or methods.
A struct can be declared using the type keyword and the struct keyword, followed by a list of fields inside curly braces. An example of a real-world entity represented as a struct is an address with fields such as name, street, city, state, and Pincode.package main
import (
"fmt"
)
func main()
// Declaring a struct type Address
type Address struct
// Creating an Address struct using a struct literal
var addr1 = Address
var addr2 = Address
Syntax
type
keyword followed by the name of the type and the struct
keyword. Inside the curly braces, we list the fields of the struct, each with a name and a type.// Declaring a named struct type Employee
type Employee struct
Named struct
// Creating an Employee struct using a struct literal
employee1 := Employee
// Creating an Employee struct using a list of values
employee2 := Employee
Anon structs
// Creating an anonymous struct
employee3 := struct
employee4 := struct
Assigning fields of a struct
// Accessing fields of a struct
fmt.Println(employee1.FirstName) // prints Soumendra
fmt.Println(employee2.Age) // prints 50
fmt.Println(employee3.Salary) // prints 30000
fmt.Println(employee4.Salary) // prints 70000
// Modifying or assigning values to fields
employee1.Salary = 60000 // modifying field value
employee2.LastName = "Gandhi" // assigning new value to field
fmt.Println(employee1.Salary) // prints 60000
fmt.Println(employee2.LastName) // prints Gandhi
Nested struct
package main
import "fmt"
type Address struct
type Employee struct
func main()
Conclusion
Related Articles