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
✗ Incorrect
The * operator dereferences the pointer, giving access to the value stored at the memory address.
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
✗ Incorrect
Passing a pointer lets the function change the original variable by accessing its memory address.
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
✗ Incorrect
Pointers let you pass the address of the struct, avoiding expensive copying of large data.
How do you get the memory address of a variable x in Go?
A*x
Bx*
Cx&
D&x
✗ Incorrect
The & operator returns the memory address of the variable.
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
✗ Incorrect
Dereferencing a nil pointer causes a runtime panic because there is no valid memory address.
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.
Practice
(1/5)
1. What does a pointer in Go store?
easy
A. The memory address of a variable
B. The value of a variable
C. The type of a variable
D. The size of a variable
Solution
Step 1: Understand what a pointer represents
A pointer holds the location in memory where a variable is stored, not the value itself.
Step 2: Differentiate pointer from value
The value is stored at the address the pointer points to, not inside the pointer variable.
Final Answer:
The memory address of a variable -> Option A
Quick Check:
Pointers store addresses = B [OK]
Hint: Pointers hold addresses, not values directly [OK]
Common Mistakes:
Confusing pointer with the actual value
Thinking pointer stores variable type
Assuming pointer stores variable size
2. Which of the following is the correct way to declare a pointer to an int in Go?
easy
A. var p int*
B. var p &int
C. var p *int
D. var *p int
Solution
Step 1: Recall Go pointer syntax
In Go, a pointer type is written as *Type, so a pointer to int is *int.
Step 2: Check variable declaration format
The variable name comes after var, then the type. So correct is var p *int.
Final Answer:
var p *int -> Option C
Quick Check:
Pointer syntax is var name *Type = D [OK]
Hint: Pointer type uses * before type, not after variable [OK]
Common Mistakes:
Placing * after variable name
Using & instead of * for pointer type
Incorrect order of * and variable name
3. What is the output of this Go code?
func main() {
x := 10
p := &x
*p = 20
fmt.Println(x)
}
medium
A. 10
B. Memory address of x
C. Compilation error
D. 20
Solution
Step 1: Understand pointer assignment
p points to x's address. Changing *p changes the value at x's address.
Step 2: Trace value change
*p = 20 sets x to 20. So printing x outputs 20.
Final Answer:
20 -> Option D
Quick Check:
Pointer changes original value = 20 [OK]
Hint: Changing *p changes original variable value [OK]
Common Mistakes:
Thinking x remains 10 after *p = 20
Printing pointer address instead of value
Assuming compilation error due to pointer
4. Identify the error in this Go code snippet:
func updateValue(p *int) {
*p = 100
}
func main() {
var p *int
updateValue(p)
}
medium
A. Syntax error in function declaration
B. p is nil and dereferenced causing runtime panic
C. updateValue should return an int
D. No error, code runs fine
Solution
Step 1: Check pointer initialization
p is declared but not assigned an address, so it is nil.
Step 2: Analyze dereferencing nil pointer
updateValue dereferences *p, causing a runtime panic because p is nil.
Final Answer:
p is nil and dereferenced causing runtime panic -> Option B
Quick Check:
Dereferencing nil pointer causes panic = A [OK]
Hint: Never dereference nil pointers without initialization [OK]