A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. Go language allows nested structure. A structure which is the field of another structure is known as Nested Structure. Or in other words, a structure within another structure is known as a Nested Structure. Syntax:Â
type struct_name_1 struct{
// Fields
}
type struct_name_2 struct{
variable_name struct_name_1
}
Let us discuss this concept with the help of the examples:Â
Example 1:Â
C
// Golang program to illustrate// the nested structurepackage mainÂ
import "fmt"Â
// Creating structuretype Author struct {    name  string    branch string    year  int}Â
// Creating nested structuretype HR struct {Â
    // structure as a field    details Author}Â
func main() {Â
    // Initializing the fields    // of the structure    result := HR{             details: Author{"Sona", "ECE", 2013},    }Â
    // Display the values    fmt.Println("\nDetails of Author")    fmt.Println(result)} |
Output:
Details of Author
{{Sona ECE 2013}}
Example 2:Â
C
// Golang program to illustrate// the nested structurepackage mainÂ
import "fmt"Â
// Creating structuretype Student struct {    name  string    branch string    year  int}Â
// Creating nested structuretype Teacher struct {    name   string    subject string    exp    int    details Student}Â
func main() {Â
    // Initializing the fields    // of the structure    result := Teacher{        name:   "Suman",        subject: "Java",        exp:    5,        details: Student{"Bongo", "CSE", 2},    }Â
    // Display the values    fmt.Println("Details of the Teacher")    fmt.Println("Teacher's name: ", result.name)    fmt.Println("Subject: ", result.subject)    fmt.Println("Experience: ", result.exp)Â
    fmt.Println("\nDetails of Student")    fmt.Println("Student's name: ", result.details.name)    fmt.Println("Student's branch name: ", result.details.branch)    fmt.Println("Year: ", result.details.year)} |
Output:
Details of the Teacher Teacher's name: Suman Subject: Java Experience: 5 Details of Student Student's name: Bongo Student's branch name: CSE Year: 2
Example 3:
In Go, a structure can have fields that are themselves structures, which are called nested structures. Here is an example of a struct that has a nested struct:
Go
package mainÂ
import (Â Â Â Â "fmt")Â
type Address struct {    Street    string    City      string    State     string    PostalCode string}Â
type Person struct {    FirstName string    LastName string    Age      int    Address  Address}Â
func main() {    p := Person{        FirstName: "John",        LastName: "Doe",        Age:      30,        Address: Address{            Street:    "123 Main St",            City:      "Anytown",            State:     "CA",            PostalCode: "12345",        },    }Â
    fmt.Println(p.FirstName, p.LastName)    fmt.Println("Age:", p.Age)    fmt.Println("Address:")    fmt.Println("Street:", p.Address.Street)    fmt.Println("City:", p.Address.City)    fmt.Println("State:", p.Address.State)    fmt.Println("Postal Code:", p.Address.PostalCode)} |
Output:
John Doe
Age: 30
Address:
Street: 123 Main St
City: Anytown
State: CA
Postal Code: 12345
Â
Here, we define two struct types: Person and Address. Person has a nested struct field Address. In the main function, we create a new Person instance with an Address field. Then, we print out the values of various fields of the Person and Address structs using dot notation to access the nested fields.
