Golang: Structs Explained

Glitch
2 min readMar 15, 2023

--

Go is a statically typed programming language that has been designed to make it easy to write simple, efficient, and reliable code. One of the features that makes Go such a powerful language is its support for structs. In this blog, we will explore what structs are and how they can be used in Go.

What are structs?
A struct is a composite data type in Go that allows you to group together zero or more values of different data types. In other words, it is a collection of fields that can have different data types. Structs are similar to classes in object-oriented programming, but they do not have methods.

In Go, you can define a struct using the type keyword followed by the name of the struct and its fields. Here's an example:
type Person struct {
Name string
Age int
Address Address
}

type Address struct {
Street string
City string
State string
Zipcode int
}
In this example, we have defined two structs, Person and Address. The Person struct has three fields: Name, Age, and Address. The Address struct has four fields: Street, City, State, and Zipcode.

Creating and initializing structs
To create a struct, you use the new keyword followed by the name of the struct. This will allocate memory for the struct and return a pointer to the struct. You can then access the fields of the struct using the dot notation.
person := new(Person)
person.Name = "John"
person.Age = 30
person.Address = Address{Street: "123 Main St", City: "New York", State: "NY", Zipcode: 10001}
Alternatively, you can use a struct literal to initialize a struct. A struct literal is a way of creating a struct and initializing its fields in one line of code.
person := Person{
Name: "John",
Age: 30,
Address: Address{
Street: "123 Main St",
City: "New York",
State: "NY",
Zipcode: 10001,
},
}
Accessing fields of a struct
To access the fields of a struct, you use the dot notation. Here’s an example:
fmt.Println(person.Name)
fmt.Println(person.Age)
fmt.Println(person.Address.Street)
fmt.Println(person.Address.City)
fmt.Println(person.Address.State)
fmt.Println(person.Address.Zipcode)
Struct embedding
In Go, you can embed one struct into another struct. This is similar to inheritance in object-oriented programming. When you embed a struct, the fields and methods of the embedded struct are automatically promoted to the outer struct. Here’s an example:
type Employee struct {
Person
EmployeeID int
Salary float64
}

employee := Employee{
Person: Person{
Name: "John",
Age: 30,
Address: Address{
Street: "123 Main St",
City: "New York",
State: "NY",
Zipcode: 10001,
},
},
EmployeeID: 1234,
Salary: 50000.00,
}

fmt.Println(employee.Name)
fmt.Println(employee.Age)
fmt.Println(employee.Address.Street)
fmt.Println(employee.Address.City)
fmt.Println(employee.Address.State)
fmt.Println(employee.Address.Zipcode)
fmt.Println(employee.EmployeeID)
fmt.Println(employee.Salary)
In this example, we have defined a new struct called Employee that embeds the Person struct. The Employee struct also has two additional fields, EmployeeID and Salary.

--

--

No responses yet