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 access a field named
Name in a struct variable person?You access it using the dot notation:
person.Name. This retrieves the value stored in the Name field of the person struct.Click to reveal answer
intermediate
Given a pointer to a struct
p *Person, how do you access the Age field?You can access it using
p.Age. Go automatically dereferences the pointer when accessing fields, so you don't need to write (*p).Age.Click to reveal answer
beginner
What happens if you try to access a field that does not exist in a struct?
The Go compiler will give an error saying the field does not exist. You must only access fields defined in the struct type.
Click to reveal answer
beginner
Why is using structs useful in Go programming?
Structs let you group related data together, making your code organized and easier to understand. They help model real-world objects with multiple properties.Click to reveal answer
How do you access the field
Title of a struct variable book?✗ Incorrect
In Go, struct fields are accessed using dot notation like
book.Title.If
p is a pointer to a struct, which is correct to access field Age?✗ Incorrect
Go automatically dereferences pointers when accessing struct fields, so
p.Age works.What error occurs if you try to access a non-existent field in a struct?
✗ Incorrect
The Go compiler reports an error if you try to access a field not defined in the struct.
Which of these is a valid way to define a struct in Go?
✗ Incorrect
In Go, structs are defined using the syntax: type Name struct { fields }.
Why use structs in Go?
✗ Incorrect
Structs group related data fields into one unit, modeling real-world objects.
Explain how to access fields in a Go struct and how pointers to structs affect this access.
Think about how you write <code>person.Name</code> and how Go lets you use <code>p.Age</code> if p is a pointer.
You got /3 concepts.
Describe what happens if you try to access a field that does not exist in a Go struct.
Remember Go checks field names when you compile your program.
You got /3 concepts.