0
0
Goprogramming~5 mins

Defining structs 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 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?
Atype
Bstruct
Cclass
Dvar
How do you access a field named 'Age' in a struct variable 'p'?
Ap[Age]
Bp->Age
Cp.Age
Dp.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
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
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 }
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.