A nested struct is a struct type that contains another struct as one of its fields. It helps organize related data in a clear, hierarchical way.
Click to reveal answer
beginner
How do you access a field inside a nested struct in Go?
You use dot notation to access fields inside nested structs. For example, if you have struct A containing struct B, and B has field C, you access it as A.B.C.
Click to reveal answer
intermediate
Why use nested structs instead of flat structs?
Nested structs group related data together, making code easier to read and maintain. It models real-world relationships better, like an Address inside a Person.
Click to reveal answer
intermediate
What happens if you embed a struct anonymously inside another struct?
Anonymous embedding lets you include a struct's fields directly in the outer struct, so you can access them without specifying the inner struct name.
Click to reveal answer
beginner
Show a simple example of a nested struct in Go.
type Address struct {
City string
Zip string
}
type Person struct {
Name string
Address Address
}
Here, Person has a nested struct Address.
Click to reveal answer
In Go, how do you define a nested struct field?
ABy creating a pointer to a struct outside the struct
BBy using inheritance like in other languages
CBy using interfaces only
DBy declaring a struct type as a field inside another struct
✗ Incorrect
Nested structs are defined by declaring a struct type as a field inside another struct.
How do you access the 'City' field in this nested struct: type Person struct { Address struct { City string } }?
APerson.Address[City]
BPerson.City
CPerson.Address.City
DPerson->Address->City
✗ Incorrect
You access nested fields using dot notation: Person.Address.City.
What is the benefit of using nested structs in Go?
AThey allow grouping related data logically
BThey automatically generate methods
CThey replace the need for functions
DThey make variables global
✗ Incorrect
Nested structs help group related data logically, improving code clarity.
What does anonymous struct embedding allow you to do?
AAccess embedded struct fields directly without the struct name
BHide fields from other packages
CCreate private methods
DPrevent struct copying
✗ Incorrect
Anonymous embedding lets you access embedded struct fields directly.
Which of these is a correct nested struct declaration in Go?
Atype Car struct { Engine []int }
Btype Car struct { Engine struct { Power int } }
Ctype Car struct { Engine string }
Dtype Car struct { Engine int }
✗ Incorrect
Option B shows a struct Engine nested inside Car, which is a nested struct.
Explain what nested structs are in Go and how you access their fields.
Think about how one struct can hold another struct as a field.
You got /3 concepts.
Describe the difference between named nested structs and anonymous struct embedding in Go.
Consider how you write the struct field and how you access its fields.
You got /3 concepts.
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
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 A
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
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 C
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
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 prints p.Address.City, which is "Paris".
Final Answer:
Paris -> Option B
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
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 A
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
}