![]() |
VOOZH | about |
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:
Output:
Details of Author
{{Sona ECE 2013}}
Example 2:
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
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:
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.