A struct in Go is a way to group related data together into a single unit, like a container with named fields. It helps organize information about something, like a person or a car.
Click to reveal answer
beginner
How do you define a struct type in Go?
You use the keyword type followed by the struct name and the keyword struct with fields inside curly braces. For example:
type Person struct {
Name string
Age int
}
Click to reveal answer
beginner
How do you create a new struct variable in Go?
You can create a new struct variable by declaring it with the struct type and then filling its fields. For example:
var p Person
p.Name = "Alice"
p.Age = 30
Click to reveal answer
beginner
What is a struct literal in Go?
A struct literal lets you create and fill a struct in one step. For example:
p := Person{Name: "Bob", Age: 25}
This creates a Person with Name "Bob" and Age 25.
Click to reveal answer
intermediate
Can structs in Go have methods?
Yes! You can write functions called methods that work with structs. This helps you add behavior to your data, like making a Person say hello.
Click to reveal answer
Which keyword is used to define a new struct type in Go?
Atype
Bstruct
Cclass
Dvar
✗ Incorrect
You use the keyword type to define a new struct type, followed by the struct name and the struct keyword.
How do you access a field named 'Age' in a struct variable 'p'?
Ap[Age]
Bp->Age
Cp.Age
Dp.Age()
✗ Incorrect
You access struct fields using dot notation, like p.Age.
What does this code do?
p := Person{Name: "Eve", Age: 22}
ACalls a method on Person
BCreates a Person struct with Name 'Eve' and Age 22
CDefines a new struct type
DDeclares a pointer to Person
✗ Incorrect
This is a struct literal creating a Person with the given field values.
Can you add functions that work specifically with a struct type?
AYes, these are called methods
BNo, Go structs cannot have functions
COnly if you use interfaces
DOnly with pointers
✗ Incorrect
Go allows you to define methods that are functions tied to a struct type.
Which of these is the correct way to define a struct with fields 'Name' (string) and 'Age' (int)?
Avar Person = { Name: string, Age: int }
Bstruct Person { string Name; int Age }
Cclass Person { Name: string; Age: int }
Dtype Person struct { Name string; Age int }
✗ Incorrect
Option D uses correct Go syntax for defining a struct type.
Explain how to define a struct type and create a variable of that type in Go.
Think about how you group data and then use it.
You got /5 concepts.
Describe what a struct literal is and how it helps when working with structs.
It's like filling a form all at once.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of defining a struct in Go?
easy
A. To create a new function
B. To group related data fields into a single unit
C. To declare a package
D. To import external libraries
Solution
Step 1: Understand the role of structs
Structs are used to group related data fields together in one unit, like a blueprint for an object.
Step 2: Compare with other options
Functions, packages, and imports serve different purposes unrelated to grouping data.
Final Answer:
To group related data fields into a single unit -> Option B
Quick Check:
Struct = grouped data [OK]
Hint: Structs bundle related info like a real-world object [OK]
Common Mistakes:
Confusing structs with functions
Thinking structs declare packages
Mixing structs with imports
2. Which of the following is the correct way to define a struct named Person with fields Name (string) and Age (int)?
easy
A. type Person { Name string; Age int }
B. struct Person { Name string; Age int }
C. type Person struct { Name string; Age int }
D. Person struct { Name string; Age int }
Solution
Step 1: Recall Go struct syntax
In Go, structs are defined using the syntax: type Name struct { fields }.
Step 2: Check each option
type Person struct { Name string; Age int } matches the correct syntax. Options A, B, and C miss the 'type' keyword or 'struct' keyword placement.
Final Answer:
type Person struct { Name string; Age int } -> Option C
Quick Check:
Correct struct syntax = type Person struct { Name string; Age int } [OK]
Hint: Remember: 'type Name struct { fields }' [OK]
Common Mistakes:
Omitting 'type' keyword
Missing 'struct' keyword
Incorrect order of keywords
3. What will be the output of the following Go code?
package main
import "fmt"
type Car struct {
Brand string
Year int
}
func main() {
c := Car{Brand: "Toyota", Year: 2020}
fmt.Println(c.Brand)
}
medium
A. Car{Brand: "Toyota", Year: 2020}
B. 2020
C. Compilation error
D. Toyota
Solution
Step 1: Understand struct initialization
The struct Car is initialized with Brand="Toyota" and Year=2020.
Step 2: Identify printed field
The code prints c.Brand, which is "Toyota".
Final Answer:
Toyota -> Option D
Quick Check:
Print c.Brand = Toyota [OK]
Hint: Print field with dot: variable.FieldName [OK]
Common Mistakes:
Printing the whole struct instead of a field
Confusing field names
Expecting Year instead of Brand
4. Identify the error in the following Go code snippet:
package main
import "fmt"
type Book struct {
Title string
Pages int
}
func main() {
b := Book{Title: "Go Guide", Pages: "300"}
fmt.Println(b)
}
medium
A. Pages field has wrong type, should be int not string
B. Missing import for fmt package
C. Struct fields must be lowercase
D. Cannot print struct directly
Solution
Step 1: Check field types in struct initialization
The Pages field is declared as int but assigned a string "300" which is a type mismatch.
Step 2: Verify other possible errors
Import for fmt package is present. Struct fields can be uppercase. Printing structs directly is allowed.
Final Answer:
Pages field has wrong type, should be int not string -> Option A
Quick Check:
Type mismatch in struct field assignment [OK]
Hint: Match field types exactly when initializing structs [OK]
Common Mistakes:
Assigning string to int field
Expecting compilation error from printing struct
Thinking struct fields must be lowercase
5. Given the struct definition:
type Employee struct {
Name string
Salary int
Active bool
}
Which of the following code snippets correctly creates a slice of Employee structs with only active employees having salary above 50000?
hard
A. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}}
var filtered []Employee
for _, e := range emps {
if e.Active && e.Salary > 50000 {
filtered = append(filtered, e)
}
}
B. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}}
filtered := []Employee{}
for i := range emps {
if emps[i].Salary > 50000 {
filtered = append(filtered, emps[i])
}
}
C. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}}
filtered := []Employee{}
for _, e := range emps {
if e.Active || e.Salary > 50000 {
filtered = append(filtered, e)
}
}
D. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}}
filtered := []Employee{}
for _, e := range emps {
if e.Active && e.Salary < 50000 {
filtered = append(filtered, e)
}
}
Solution
Step 1: Understand filtering criteria
We want only employees who are active and have salary above 50000.
Step 2: Check each code snippet logic
emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}}
var filtered []Employee
for _, e := range emps {
if e.Active && e.Salary > 50000 {
filtered = append(filtered, e)
}
} uses 'e.Active && e.Salary > 50000' which matches criteria. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}}
filtered := []Employee{}
for i := range emps {
if emps[i].Salary > 50000 {
filtered = append(filtered, emps[i])
}
} misses active check. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}}
filtered := []Employee{}
for _, e := range emps {
if e.Active || e.Salary > 50000 {
filtered = append(filtered, e)
}
} uses '||' which includes inactive employees. emps := []Employee{{"Alice", 60000, true}, {"Bob", 45000, true}, {"Carol", 70000, false}}
filtered := []Employee{}
for _, e := range emps {
if e.Active && e.Salary < 50000 {
filtered = append(filtered, e)
}
} uses salary < 50000 which is wrong.
Final Answer:
The code that filters using e.Active && e.Salary > 50000 -> Option A
Quick Check:
Filter with '&&' for both conditions [OK]
Hint: Use && to combine multiple conditions correctly [OK]
Common Mistakes:
Using || instead of && for filtering
Ignoring the Active field in filter
Using wrong salary comparison operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.