0
0
Goprogramming~5 mins

Common pointer use cases in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a pointer in Go?
A pointer in Go is a variable that stores the memory address of another variable. It allows you to directly access and modify the value stored at that address.
Click to reveal answer
beginner
Why use pointers to modify function arguments in Go?
Using pointers as function arguments allows the function to modify the original variable's value, not just a copy. This is useful for changing data outside the function's scope.
Click to reveal answer
intermediate
How do pointers help with performance in Go?
Pointers avoid copying large data structures by passing their memory address instead. This reduces memory use and speeds up the program.
Click to reveal answer
intermediate
What is the use of pointers with structs in Go?
Pointers to structs let you modify the struct fields directly and efficiently pass large structs without copying all their data.
Click to reveal answer
beginner
How do you declare and use a pointer in Go?
Declare a pointer with * before the type, e.g., var p *int. Use & to get the address of a variable, e.g., p = &x. Use *p to access or change the value at that address.
Click to reveal answer
What does the * operator do when used with a pointer variable in Go?
AAccesses the value stored at the pointer's address
BDeclares a new pointer variable
CGets the address of a variable
DCreates a copy of the variable
Why might you pass a pointer to a function instead of a value in Go?
ATo allow the function to modify the original variable
BTo make a copy of the variable
CTo prevent the function from changing the variable
DTo increase the size of the variable
Which of these is a benefit of using pointers with large structs?
AAutomatically initializes all fields to zero
BAvoids copying the entire struct, improving performance
CMakes the struct immutable
DPrevents access to struct fields
How do you get the memory address of a variable x in Go?
A*x
Bx*
Cx&
D&x
What happens if you try to dereference a nil pointer in Go?
AIt creates a new variable
BIt returns zero value
CThe program panics (runtime error)
DIt compiles but does nothing
Explain three common use cases for pointers in Go and why they are useful.
Think about how pointers let you change data and save memory.
You got /3 concepts.
    Describe how to declare, assign, and use a pointer variable in Go with an example.
    Use a simple int variable example.
    You got /3 concepts.