0
0
Goprogramming~5 mins

Creating struct values in Go - Quick Revision & Summary

Choose your learning style9 modes available
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?
APerson{Name: "John", Age: 40}
BPerson("John", 40)
Cnew Person("John", 40)
DPerson->Name = "John"
What is the zero value of an omitted int field in a struct?
A"" (empty string)
Bnil
Cundefined
D0
How do you create a pointer to a struct value in Go?
APerson&{Name: "Anna", Age: 22}
B*Person{Name: "Anna", Age: 22}
C&Person{Name: "Anna", Age: 22}
Dnew Person{Name: "Anna", Age: 22}
Which of these is a valid way to create a struct value using positional fields?
APerson{"Mike", 35}
BPerson{Name: "Mike", Age: 35}
CPerson(Age: 35, Name: "Mike")
DPerson["Mike", 35]
What is the benefit of using named fields over positional fields when creating struct values?
AMakes the code run faster
BImproves readability and reduces errors
CAllows omitting required fields
DAutomatically initializes pointers
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.