We discussed Golang struct in our previous post, and this post talks about the nested structs in Golang.
We can also have a field in the struct that itself is a struct. These kinds of structs will be called nested structs, as one struct contains the other. The nesting can be done up to any level(s).
Let’s look at the 1-level nesting right now. We will create a Company struct containing another struct, “Employee“.
Employee struct
type Employee struct {
firstName string
lastName string
age int
height float64
}
Company struct
type Company struct {
department string
employee Employee
Now, we can access the Employee struct from the Company struct using the infamous dot operator.
package main
import (
"fmt"
)
type Employee struct {
firstName string
lastName string
age int
height float64
}
type Company struct {
department string
employee Employee
}
func main() {
employee1 := Employee{"codekru", "website", 22, 167.12} // defining the employee struct
company1 := Company{"CS", employee1} // defining the company struct
fmt.Println("Employee Details:", company1.employee) // accessing the employee struct using the dot operator
}
Output –
Employee Details: {codekru website 22 167.12}
How to access the fields of a nested struct?
We can access the individual fields of the inner struct from the outer struct by using the chaining of the dot operator. So, we can use the company1.employee.age to access the age field of the Employee from the company struct.
package main
import (
"fmt"
)
type Employee struct {
firstName string
lastName string
age int
height float64
}
type Company struct {
department string
employee Employee
}
func main() {
employee1 := Employee{"codekru", "website", 22, 167.12} // defining the employee struct
company1 := Company{"CS", employee1} // defining the company struct
fmt.Println("Employee age:", company1.employee.age) // accessing the employee struct using the dot operator
}
Output –
Employee age: 22
Multi-level Nested Structs
Let’s take one more struct, “Address“, which will be a field of the “Employee” struct. So, here Company struct contains the Employee struct, and the Employee struct further nests the Address struct. This is what we call a Multi-level Nested Structs.
The Company struct can access the fields of the Address struct using the dot operator.
package main
import (
"fmt"
)
type Address struct {
state string
country string
}
type Employee struct {
firstName string
lastName string
age int
height float64
address Address
}
type Company struct {
department string
employee Employee
}
func main() {
addressOfEmployee := Address{"Delhi", "India"} // defining the address struct
employee1 := Employee{"codekru", "website", 22, 167.12, addressOfEmployee} // defining the employee struct
company := Company{"CS", employee1} // defining the company struct
fmt.Println("Employee address:", company.employee.address) // accessing the address using the dot operator
fmt.Println("Employee Country:", company.employee.address.country)
}
Output –
Employee address: {Delhi India}
Employee Country: India
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.