0
0
Goprogramming~5 mins

Struct pointers in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a struct pointer in Go?
A struct pointer in Go is a variable that holds the memory address of a struct. It allows you to access and modify the struct's fields directly through the pointer.
Click to reveal answer
beginner
How do you declare a pointer to a struct in Go?
You declare a pointer to a struct by using the * symbol before the struct type. For example: var p *Person declares a pointer to a Person struct.
Click to reveal answer
beginner
How do you access struct fields using a pointer in Go?
You can access struct fields using the pointer with the dot operator directly, like p.Name. Go automatically dereferences the pointer for you.
Click to reveal answer
intermediate
What is the difference between a struct value and a struct pointer when passing to a function?
Passing a struct value copies the entire struct, so changes inside the function don't affect the original. Passing a struct pointer passes the address, so changes inside the function modify the original struct.
Click to reveal answer
intermediate
How do you create a new struct pointer with initialized values?
You can use the new keyword or take the address of a struct literal. For example: p := new(Person) or p := &Person{Name: "Alice"}.
Click to reveal answer
Which symbol is used to declare a pointer to a struct in Go?
A*
B&
C#
D$
How do you access a field 'Name' of a struct through a pointer 'p' in Go?
A(*p).Name
Bp->Name
Cp.Name
Dp*Name
What happens when you pass a struct pointer to a function?
AThe function gets a copy of the struct
BThe function cannot access the struct fields
CThe struct is converted to a string
DThe function can modify the original struct
Which of these creates a new struct pointer with initialized values?
Ap := Person{Name: "Bob"}
Bp := &Person{Name: "Bob"}
Cp := *Person{Name: "Bob"}
Dp := new(Person{Name: "Bob"})
What does the 'new' keyword do when used with a struct type?
ACreates a pointer to a zero-valued struct
BDeletes the struct
CCreates a struct value
DCopies the struct
Explain how struct pointers work in Go and how you can use them to modify struct data.
Think about how pointers let you work directly with the original data.
You got /3 concepts.
    Describe two ways to create a struct pointer with initial values in Go.
    One way uses 'new', the other uses & with curly braces.
    You got /2 concepts.