0
0
Goprogramming~10 mins

Nested structs in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested structs
Define Outer Struct
Define Inner Struct
Create Outer Struct Instance
Assign Inner Struct to Outer
Access Inner Struct Fields
Use Nested Data
This flow shows how to define a struct inside another struct, create instances, assign values, and access nested fields.
Execution Sample
Go
package main

import "fmt"

type Address struct {
    City string
    Zip  int
}

type Person struct {
    Name    string
    Address Address
}

func main() {
    p := Person{Name: "Alice", Address: Address{City: "NYC", Zip: 10001}}
    fmt.Println(p.Name, p.Address.City, p.Address.Zip)
}
This code defines two structs, Person and Address, nests Address inside Person, creates a Person instance, and prints nested fields.
Execution Table
StepActionVariable/FieldValueNotes
1Define struct AddressAddressstruct with City(string), Zip(int)Struct type created
2Define struct PersonPersonstruct with Name(string), Address(Address)Struct type created with nested struct
3Create Person instance pp.Name"Alice"Name field set
3Create Person instance pp.Address.City"NYC"Nested City field set
3Create Person instance pp.Address.Zip10001Nested Zip field set
4Print p.NameOutput"Alice"Access outer struct field
4Print p.Address.CityOutput"NYC"Access nested struct field
4Print p.Address.ZipOutput10001Access nested struct field
5End of main--Program ends
💡 Program ends after printing nested struct fields
Variable Tracker
VariableStartAfter Step 3Final
p.Name"""Alice""Alice"
p.Address.City"""NYC""NYC"
p.Address.Zip01000110001
Key Moments - 2 Insights
Why do we write p.Address.City instead of just p.City?
Because City is inside the nested Address struct, so we must access it through p.Address as shown in execution_table rows 4-7.
Can we assign Address fields directly without creating Address struct first?
No, we must create the Address struct instance first and assign it to p.Address, as shown in step 3 of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p.Address.Zip after step 3?
A10001
B0
C"NYC"
D"Alice"
💡 Hint
Check execution_table row 5 where p.Address.Zip is set to 10001
At which step do we assign the Name field of p?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 where p.Name is set to "Alice"
If we tried to print p.City directly, what would happen?
AIt prints the city name
BIt prints empty string
CCompilation error: p.City undefined
DIt prints zero
💡 Hint
Refer to key_moments question 1 about accessing nested fields
Concept Snapshot
Nested structs in Go:
- Define inner struct type
- Use inner struct as field in outer struct
- Create outer struct instance with inner struct values
- Access nested fields with dot notation (outer.inner.field)
- Nested structs group related data clearly
Full Transcript
This example shows how to use nested structs in Go. First, we define an Address struct with City and Zip fields. Then, we define a Person struct that includes a Name and an Address field. We create a Person instance named p, setting Name to "Alice" and Address to an Address instance with City "NYC" and Zip 10001. We print p.Name, p.Address.City, and p.Address.Zip. The execution table traces each step, showing how fields are assigned and accessed. Key points include accessing nested fields with p.Address.City and the need to create the nested struct instance before assigning it. The quiz checks understanding of these steps. This helps organize complex data clearly in Go programs.