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:
// Golang program to illustrate // the nested structure package main import "fmt" // Creating structure type Author struct { name string branch string year int} // Creating nested structure type 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:
// Golang program to illustrate // the nested structure package main import "fmt" // Creating structure type Student struct { name string branch string year int} // Creating nested structure type 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
Recommended Posts:
- Golang Program that Removes Duplicates Using Nested Loops
- Structure Equality in Golang
- Anonymous Structure and Field in Golang
- Function as a Field in Golang Structure
- Promoted Fields in Golang Structure
- Promoted Methods in Golang Structure
- Checking if structure is empty or not in Golang
- Printing structure variables in console in Golang
- Go Decision Making (if, if-else, Nested-if, if-else-if)
- How to convert a slice of bytes in uppercase in Golang?
- Golang program that uses fallthrough keyword
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- How to check equality of slices of bytes in Golang?
- atomic.AddInt64() Function in Golang With Examples
- atomic.StoreInt64() Function in Golang With Examples
- reflect.FieldByIndex() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- bits.Sub() Function in Golang with Examples
- How to convert a slice of bytes in lowercase in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

