Nested structs in Go - Time & Space Complexity
When working with nested structs in Go, it's important to understand how accessing and processing them affects the time your program takes.
We want to see how the time grows when we work through nested structs.
Analyze the time complexity of the following code snippet.
type Address struct {
City string
State string
}
type Person struct {
Name string
Age int
Address Address
}
func printCities(people []Person) {
for _, p := range people {
fmt.Println(p.Address.City)
}
}
This code loops through a list of people and prints the city from their nested Address struct.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the slice of people.
- How many times: Once for each person in the list.
As the number of people increases, the number of times we print cities grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly with the number of people.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of people.
[X] Wrong: "Accessing nested structs makes the time complexity multiply or become slower than linear."
[OK] Correct: Accessing fields inside nested structs is a direct operation and does not add extra loops, so it does not increase the overall time complexity beyond the main loop.
Understanding how nested structs affect time helps you explain your code clearly and shows you know how data structures impact performance.
"What if we added a nested loop inside the printCities function to loop over multiple addresses per person? How would the time complexity change?"
Practice
nested structs in Go?Solution
Step 1: Understand struct grouping
Nested structs allow one struct to contain another, grouping related data logically.Step 2: Compare options
Creating multiple independent structs, making the program run faster, or avoiding functions do not describe grouping data inside structs.Final Answer:
To group related data together inside another struct -> Option AQuick Check:
Nested structs = Group related data [OK]
- Confusing nested structs with separate structs
- Thinking nested structs improve speed
- Believing nested structs replace functions
City inside a nested struct Address within struct Person?Solution
Step 1: Recall Go syntax for nested structs
Access nested fields using dot notation: Outer.Inner.Field.Step 2: Evaluate options
person.Address.City uses correct dot notation. Arrow syntax (->) is invalid in Go. person.City.Address reverses the order.Final Answer:
person.Address.City -> Option CQuick Check:
Dot notation = person.Address.City [OK]
- Using arrow (->) instead of dot (.)
- Reversing the order of fields
- Trying to access nested fields directly without parent
package main
import "fmt"
type Address struct {
City string
Zip int
}
type Person struct {
Name string
Address Address
}
func main() {
p := Person{Name: "Anna", Address: Address{City: "Paris", Zip: 75000}}
fmt.Println(p.Address.City)
}Solution
Step 1: Understand struct initialization
Person struct has Name and Address fields. Address is initialized with City "Paris" and Zip 75000.Step 2: Identify printed field
The code printsp.Address.City, which is "Paris".Final Answer:
Paris -> Option BQuick Check:
p.Address.City = Paris [OK]
- Printing the wrong field like Name or Zip
- Confusing struct field names
- Expecting the whole struct to print
package main
import "fmt"
type Engine struct {
Power int
}
type Car struct {
Model string
Engine Engine
}
func main() {
c := Car{Model: "Sedan"}
fmt.Println(c.Engine.Power)
}Solution
Step 1: Check struct definitions
Engine and Car structs are defined correctly. Car has Engine field.Step 2: Analyze initialization and print
Car instance c is created with Model only. Engine field is zero-valued (Power = 0). Printing c.Engine.Power prints 0 without error.Final Answer:
c.Engine.Power is zero value, no error -> Option AQuick Check:
Uninitialized nested struct fields have zero values [OK]
- Assuming missing initialization causes error
- Thinking nested fields can't be printed
- Confusing missing fields with zero values
FullAddress that returns a formatted string combining Street, City, and Zip from nested structs?
type Location struct {
Street string
City string
}
type Address struct {
Location Location
Zip int
}Solution
Step 1: Identify nested fields
Street and City are inside Location struct, which is nested inside Address.Step 2: Access nested fields correctly
Use a.Location.Street and a.Location.City to access those fields, and a.Zip for Zip code.Step 3: Check options
Only func FullAddress(a Address) string { return a.Location.Street + ", " + a.Location.City + " " + fmt.Sprint(a.Zip) } correctly accesses nested fields with dot notation.Final Answer:
func FullAddress(a Address) string { return a.Location.Street + ", " + a.Location.City + " " + fmt.Sprint(a.Zip) } -> Option DQuick Check:
Access nested fields with dot notation [OK]
- Accessing nested fields directly without parent struct
- Mixing field names from different structs
- Forgetting to convert int Zip to string
Ready to prove it?
Pass the test and this pattern moves unknown
