Nested Struct Type
Struct can be nested by creating a Struct type using other Struct types as the type for the fields of Struct. Nesting one struct within another can be a useful way to model more complex structures.
Example
package main
import "fmt"
type Salary struct {
Basic, HRA, TA float64
}
type Employee struct {
FirstName, LastName, Email string
Age int
MonthlySalary []Salary
}
func main() {
e := Employee{
FirstName: "Mark",
LastName: "Jones",
Email: "mark@gmail.com",
Age: 25,
MonthlySalary: []Salary{
Salary{
Basic: 15000.00,
HRA: 5000.00,
TA: 2000.00,
},
Salary{
Basic: 16000.00,
HRA: 5000.00,
TA: 2100.00,
},
Salary{
Basic: 17000.00,
HRA: 5000.00,
TA: 2200.00,
},
},
}
fmt.Println(e.FirstName, e.LastName)
fmt.Println(e.Age)
fmt.Println(e.Email)
fmt.Println(e.MonthlySalary[0])
fmt.Println(e.MonthlySalary[1])
fmt.Println(e.MonthlySalary[2])
}
Most Helpful This Week
How to convert Struct fields into Map String?
How to Convert string to integer type in Go?
How to remove all line breaks from a string in Golang?
Example of Interface with Type Embedding and Method Overriding in GO language
Syntax error: unexpected <token> error in Golang
How to append struct member dynamically using Empty Interface?
Golang Program to print Floyd's triangle
Golang program for implementation LZW Data Compression and Uncompression
Go program to find Name Server (NS) record of a domain
How do you set cookies in an HTTP request with an HTTP client in Go?