0
0
Goprogramming~5 mins

Accessing struct fields in Go - Cheat Sheet & Quick Revision

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 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?
Abook:Title
Bbook.Title
Cbook[Title]
Dbook->Title
If p is a pointer to a struct, which is correct to access field Age?
Ap->Age
B*p.Age
Cp.Age
Dp[Age]
What error occurs if you try to access a non-existent field in a struct?
ACompile-time error
BRuntime panic
CSilent failure
DReturns zero value
Which of these is a valid way to define a struct in Go?
Astruct Person { Name string; Age int }
Bclass Person { Name string; Age int }
CPerson = struct(Name string, Age int)
Dtype Person struct { Name string; Age int }
Why use structs in Go?
ATo group related data together
BTo perform arithmetic operations
CTo create loops
DTo handle errors
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.