Recall & Review
beginner
What is a struct in Go?
A struct in Go is a collection of fields grouped together to form a single data type. It is used to represent complex data by combining different values under one name.
Click to reveal answer
beginner
How do you define a struct type in Go?
You define a struct type using the
type keyword followed by the struct name and the struct keyword with fields inside curly braces. Example:<br>type Person struct {
Name string
Age int
}Click to reveal answer
intermediate
What is struct embedding and why is it useful?
Struct embedding is when one struct includes another struct as a field without a name. It allows the embedded struct's fields and methods to be accessed directly, enabling composition and code reuse.
Click to reveal answer
beginner
How do you create a new instance of a struct with field values?
You create a new struct instance by specifying the struct type and initializing fields inside curly braces. Example:<br>
p := Person{Name: "Alice", Age: 30}Click to reveal answer
intermediate
What is the difference between value and pointer receivers in struct methods?
Value receivers get a copy of the struct, so changes inside the method don't affect the original. Pointer receivers get the address, so changes inside the method modify the original struct. Use pointer receivers to modify data or avoid copying large structs.
Click to reveal answer
How do you access a field named
Age from a struct variable p?✗ Incorrect
In Go, struct fields are accessed using dot notation:
p.Age.What happens if you embed a struct
Address inside another struct Person?✗ Incorrect
Embedding allows direct access to the embedded struct's fields from the outer struct.
Which keyword is used to define a new struct type?
✗ Incorrect
The
type keyword defines a new struct type, followed by the struct name and fields.Why use pointer receivers in struct methods?
✗ Incorrect
Pointer receivers allow methods to modify the original struct by receiving its address.
How do you create a struct instance with default zero values?
✗ Incorrect
Both
var p Person and p := Person{} create a struct with zero values for all fields.Explain how struct embedding works and give an example of when you might use it.
Think about how one struct can include another to reuse code.
You got /3 concepts.
Describe the difference between value and pointer receivers in struct methods and why it matters.
Consider what happens when you want to change the struct inside a method.
You got /3 concepts.