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 a record or an object with multiple properties.
Click to reveal answer
beginner
How do you create a struct value using a struct literal?
You create a struct value by specifying the struct type followed by curly braces containing field values, like:
Person{Name: "Alice", Age: 30}.Click to reveal answer
intermediate
What is the difference between named fields and positional fields when creating a struct value?
Named fields specify the field names explicitly (e.g.,
Person{Name: "Bob", Age: 25}), while positional fields provide values in the order of declaration without naming (e.g., Person{"Bob", 25}). Named fields improve readability and safety.Click to reveal answer
beginner
How do you create a pointer to a struct value?
You can create a pointer to a struct by using the address operator
& before a struct literal, like: p := &Person{Name: "Eve", Age: 28}.Click to reveal answer
intermediate
What happens if you omit some fields when creating a struct value with named fields?
Omitted fields are set to their zero values automatically. For example, if you omit an int field, it becomes 0; if you omit a string field, it becomes an empty string.
Click to reveal answer
How do you create a struct value with named fields in Go?
✗ Incorrect
Named fields require specifying the field names inside the struct literal, like Person{Name: "John", Age: 40}.
What is the zero value of an omitted int field in a struct?
✗ Incorrect
The zero value for int fields is 0 when omitted in a struct literal.
How do you create a pointer to a struct value in Go?
✗ Incorrect
Using the & operator before a struct literal creates a pointer to that struct.
Which of these is a valid way to create a struct value using positional fields?
✗ Incorrect
Positional fields provide values in order without naming, like Person{"Mike", 35}.
What is the benefit of using named fields over positional fields when creating struct values?
✗ Incorrect
Named fields make the code easier to read and help avoid mistakes by explicitly naming each field.
Explain how to create a struct value in Go using named fields and what happens if some fields are omitted.
Think about how you write the field names and what default values Go uses.
You got /3 concepts.
Describe how to create a pointer to a struct value and why you might want to use a pointer.
Pointers let you work with the original data without copying.
You got /3 concepts.