Bird
Raised Fist0
Goprogramming~20 mins

Nested structs in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Structs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested struct field access
What is the output of this Go program?
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: "Paris", Zip: 75000}}
    fmt.Println(p.Address.City)
}
A75000
BAddress{City: "Paris", Zip: 75000}
CAlice
DParis
Attempts:
2 left
💡 Hint
Look at how nested structs are accessed using dot notation.
❓ Predict Output
intermediate
2:00remaining
Output of nested struct pointer access
What will this Go program print?
Go
package main
import "fmt"
type Engine struct {
    Horsepower int
}
type Car struct {
    Model  string
    Engine *Engine
}
func main() {
    e := Engine{Horsepower: 300}
    c := Car{Model: "Mustang", Engine: &e}
    fmt.Println(c.Engine.Horsepower)
}
AMustang
Bnil
C300
D0
Attempts:
2 left
💡 Hint
Remember to dereference pointers automatically with dot notation.
🔧 Debug
advanced
2:00remaining
Identify the compilation error in nested struct initialization
This Go code does not compile. What is the cause of the error?
Go
package main
import "fmt"
type Point struct {
    X, Y int
}
type Rectangle struct {
    TopLeft, BottomRight Point
}
func main() {
    r := Rectangle{TopLeft: Point{X: 0, Y: 10}, BottomRight: Point{X: 5, Y: 0}}
    fmt.Println(r)
}
AMissing type name in nested struct literals
BCannot use composite literals inside struct initialization
CFields TopLeft and BottomRight are unexported
Dfmt.Println cannot print structs
Attempts:
2 left
💡 Hint
Check how nested structs are initialized in Go.
❓ Predict Output
advanced
2:00remaining
Output of embedded struct field promotion
What does this Go program print?
Go
package main
import "fmt"
type Contact struct {
    Email string
}
type Employee struct {
    Name    string
    Contact
}
func main() {
    e := Employee{Name: "Bob", Contact: Contact{Email: "bob@example.com"}}
    fmt.Println(e.Email)
}
AName field value: Bob
Bbob@example.com
CCompilation error: ambiguous field
DContact{Email: "bob@example.com"}
Attempts:
2 left
💡 Hint
Embedded structs promote their fields to the outer struct.
🧠 Conceptual
expert
2:00remaining
Number of fields in nested and embedded structs
Given these struct definitions, how many fields does variable 'd' of type Developer have?
Go
type Address struct {
    City string
    Zip  int
}
type Contact struct {
    Email string
    Phone string
}
type Developer struct {
    Name    string
    Address
    Contact
}
A5
B3
C4
D6
Attempts:
2 left
💡 Hint
Count all fields including embedded struct fields promoted to Developer.

Practice

(1/5)
1. What is the main purpose of using nested structs in Go?
easy
A. To group related data together inside another struct
B. To create multiple independent structs
C. To make the program run faster
D. To avoid using functions

Solution

  1. Step 1: Understand struct grouping

    Nested structs allow one struct to contain another, grouping related data logically.
  2. Step 2: Compare options

    Creating multiple independent structs, making the program run faster, or avoiding functions do not describe grouping data inside structs.
  3. Final Answer:

    To group related data together inside another struct -> Option A
  4. Quick Check:

    Nested structs = Group related data [OK]
Hint: Think of nested structs as folders inside folders [OK]
Common Mistakes:
  • Confusing nested structs with separate structs
  • Thinking nested structs improve speed
  • Believing nested structs replace functions
2. Which of the following is the correct way to access the field City inside a nested struct Address within struct Person?
easy
A. person.City.Address
B. person->Address->City
C. person.Address.City
D. person.Address->City

Solution

  1. Step 1: Recall Go syntax for nested structs

    Access nested fields using dot notation: Outer.Inner.Field.
  2. Step 2: Evaluate options

    person.Address.City uses correct dot notation. Arrow syntax (->) is invalid in Go. person.City.Address reverses the order.
  3. Final Answer:

    person.Address.City -> Option C
  4. Quick Check:

    Dot notation = person.Address.City [OK]
Hint: Use dot notation to access nested fields [OK]
Common Mistakes:
  • Using arrow (->) instead of dot (.)
  • Reversing the order of fields
  • Trying to access nested fields directly without parent
3. What will be the output of the following Go code?
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)
}
medium
A. Anna
B. Paris
C. 75000
D. Address

Solution

  1. Step 1: Understand struct initialization

    Person struct has Name and Address fields. Address is initialized with City "Paris" and Zip 75000.
  2. Step 2: Identify printed field

    The code prints p.Address.City, which is "Paris".
  3. Final Answer:

    Paris -> Option B
  4. Quick Check:

    p.Address.City = Paris [OK]
Hint: Print nested field with dot notation [OK]
Common Mistakes:
  • Printing the wrong field like Name or Zip
  • Confusing struct field names
  • Expecting the whole struct to print
4. Identify the error in this Go code snippet involving nested structs:
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)
}
medium
A. c.Engine.Power is zero value, no error
B. Car struct missing Engine field
C. Engine struct is not defined
D. Cannot print nested struct fields directly

Solution

  1. Step 1: Check struct definitions

    Engine and Car structs are defined correctly. Car has Engine field.
  2. 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.
  3. Final Answer:

    c.Engine.Power is zero value, no error -> Option A
  4. Quick Check:

    Uninitialized nested struct fields have zero values [OK]
Hint: Uninitialized nested structs have zero values [OK]
Common Mistakes:
  • Assuming missing initialization causes error
  • Thinking nested fields can't be printed
  • Confusing missing fields with zero values
5. Given the structs below, how would you write a function 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
}
hard
A. func FullAddress(a Address) string { return a.Street + ", " + a.City + " " + fmt.Sprint(a.Zip) }
B. func FullAddress(a Address) string { return a.Street + ", " + a.Location.City + " " + fmt.Sprint(a.Zip) }
C. func FullAddress(a Address) string { return a.Location.Street + ", " + a.City + " " + fmt.Sprint(a.Zip) }
D. func FullAddress(a Address) string { return a.Location.Street + ", " + a.Location.City + " " + fmt.Sprint(a.Zip) }

Solution

  1. Step 1: Identify nested fields

    Street and City are inside Location struct, which is nested inside Address.
  2. Step 2: Access nested fields correctly

    Use a.Location.Street and a.Location.City to access those fields, and a.Zip for Zip code.
  3. 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.
  4. Final Answer:

    func FullAddress(a Address) string { return a.Location.Street + ", " + a.Location.City + " " + fmt.Sprint(a.Zip) } -> Option D
  5. Quick Check:

    Access nested fields with dot notation [OK]
Hint: Use dot notation for each nested level [OK]
Common Mistakes:
  • Accessing nested fields directly without parent struct
  • Mixing field names from different structs
  • Forgetting to convert int Zip to string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard