0
0
Goprogramming~5 mins

Nested structs in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a nested struct in Go?
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
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
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
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
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 }
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.