Recall & Review
beginner
What is a struct in Go?
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:<br>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:<br>
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:<br>
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?
✗ 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'?
✗ Incorrect
You access struct fields using dot notation, like
p.Age.What does this code do?
p := Person{Name: "Eve", Age: 22}✗ 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?
✗ 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)?
✗ 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.