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:
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:
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?
Ap["Age"]
Bp.Age
Cp->Age
DAge.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?
AEmbedding is not allowed in Go.
BYou must always use Address field name to access fields.
CYou can access Address fields directly from Person.
DPerson becomes a pointer automatically.
✗ 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?
Afunc
Bstruct
Cvar
Dtype
✗ Incorrect
The type keyword defines a new struct type, followed by the struct name and fields.
Why use pointer receivers in struct methods?
ATo modify the original struct inside the method.
BTo make a copy of the struct.
CTo prevent method calls.
DPointer receivers are not allowed.
✗ 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?
ABoth A and C
Bvar p Person
Cp := Person{}
Dp := new(Person)
✗ 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.
Practice
(1/5)
1. What is the primary purpose of using a struct in Go?
easy
A. To group related data with named fields
B. To perform arithmetic operations
C. To create loops
D. To handle errors
Solution
Step 1: Understand the role of structs
Structs are used to group related data together in one unit with named fields.
Step 2: Compare with other options
Arithmetic, loops, and error handling are not the main purposes of structs.
Final Answer:
To group related data with named fields -> Option A
Quick Check:
Structs = Group related data [OK]
Hint: Structs group data fields logically [OK]
Common Mistakes:
Thinking structs are for control flow
Confusing structs with functions
Assuming structs handle errors
2. Which of the following is the correct way to declare a struct type named Person with fields Name (string) and Age (int)?
easy
A. Person := struct { Name string; Age int }
B. struct Person { Name string; Age int }
C. var Person struct Name string; Age int
D. type Person struct { Name string; Age int }
Solution
Step 1: Recall Go struct declaration syntax
In Go, a struct type is declared with the keyword type, followed by the name, then struct and fields inside braces.
Step 2: Check each option
type Person struct { Name string; Age int } matches the correct syntax. struct Person { Name string; Age int } misses type. Person := struct { Name string; Age int } uses := which is for variables, not type declaration. var Person struct Name string; Age int is invalid syntax.
Final Answer:
type Person struct { Name string; Age int } -> Option D
Quick Check:
Struct declaration uses 'type Name struct { fields }' [OK]
Hint: Use 'type Name struct { fields }' to declare structs [OK]
Common Mistakes:
Omitting 'type' keyword
Using := for type declaration
Incorrect field syntax
3. What will be the output of the following Go code?
type Point struct {
X int
Y int
}
func main() {
p := Point{X: 3, Y: 4}
p.X = 10
fmt.Println(p.X, p.Y)
}
medium
A. 10 4
B. 3 4
C. 10 0
D. 0 4
Solution
Step 1: Understand struct initialization and modification
The struct Point is initialized with X=3 and Y=4. Then p.X is changed to 10.
Step 2: Determine printed values
After modification, p.X is 10 and p.Y remains 4. So printing p.X and p.Y outputs '10 4'.
Final Answer:
10 4 -> Option A
Quick Check:
Modified field prints updated value [OK]
Hint: Changing struct fields updates their values [OK]
Common Mistakes:
Assuming original values stay after assignment
Confusing field names
Forgetting to import fmt (not shown here)
4. Identify the error in the following Go code snippet:
type Car struct {
Brand string
Year int
}
func main() {
c := Car{Brand: "Toyota", Year: "2020"}
fmt.Println(c)
}
medium
A. Missing semicolon after struct declaration
B. Year field assigned a string instead of int
C. Incorrect struct name capitalization
D. fmt.Println cannot print structs
Solution
Step 1: Check field types in struct literal
The struct Car has Year as int, but the code assigns "2020" (a string) to Year.
Step 2: Verify other options
Semicolons are optional in Go. Struct name capitalization is fine. fmt.Println can print structs.
Final Answer:
Year field assigned a string instead of int -> Option B
Quick Check:
Field types must match assigned values [OK]
Hint: Match struct field types exactly when assigning [OK]
Common Mistakes:
Assigning wrong data types to fields
Thinking semicolons are mandatory
Assuming fmt.Println can't print structs
5. Given a struct type Employee with fields Name (string), Salary (int), and Active (bool), which code snippet correctly creates a slice of active employees with salary over 50000 from a given slice emps?
hard
A. filtered := make([]Employee, 0)
for _, e := range emps {
if e.Active || e.Salary > 50000 {
filtered = append(filtered, e)
}
}
B. filtered := []Employee{}
for i := 0; i < len(emps); i++ {
if emps[i].Salary > 50000 {
filtered = append(filtered, emps[i])
}
}
C. var filtered []Employee
for _, e := range emps {
if e.Active && e.Salary > 50000 {
filtered = append(filtered, e)
}
}
D. filtered := []Employee{}
for _, e := range emps {
if e.Active && e.Salary < 50000 {
filtered = append(filtered, e)
}
}
Solution
Step 1: Understand filtering conditions
We want employees who are active AND have salary greater than 50000.
Step 2: Analyze each option's condition
var filtered []Employee
for _, e := range emps {
if e.Active && e.Salary > 50000 {
filtered = append(filtered, e)
}
} uses correct AND condition. filtered := []Employee{}
for i := 0; i < len(emps); i++ {
if emps[i].Salary > 50000 {
filtered = append(filtered, emps[i])
}
} misses Active check. filtered := make([]Employee, 0)
for _, e := range emps {
if e.Active || e.Salary > 50000 {
filtered = append(filtered, e)
}
} uses OR instead of AND. filtered := []Employee{}
for _, e := range emps {
if e.Active && e.Salary < 50000 {
filtered = append(filtered, e)
}
} uses salary less than 50000, which is wrong.
Final Answer:
var filtered []Employee
for _, e := range emps {
if e.Active && e.Salary > 50000 {
filtered = append(filtered, e)
}
} -> Option C
Quick Check:
Use AND (&&) for combined conditions [OK]
Hint: Use && to combine multiple filter conditions [OK]
Common Mistakes:
Using || instead of && for filtering
Ignoring one of the filter conditions
Filtering with wrong comparison operators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.