Nested Structs
Structs can contain other structs as fields, allowing you to represent deeply structured information.
Example
type wheel struct {
radius int
material string
}
type car struct {
brand string
frontWheel wheel
backWheel wheel
}
Accessing Nested Fields
You use the dot operator recursively to reach the values inside nested structs.
myCar := car{}
myCar.frontWheel.radius = 15
myCar.frontWheel.material = "Alloy"
fmt.Println(myCar.frontWheel.radius) // 15
Purpose
Nesting helps modularize your data. Instead of having 20 fields on a single struct, you can group related fields into smaller, reusable structs.