Pointers in Go
- A pointer stores the memory address of a variable.
&→ Gets the address of a variable.*→ Dereferences a pointer (accesses the value).
var x int = 10
ptr := &x // ptr holds the address of x
fmt.Println(*ptr) // 10 (dereferenced value)Why Use Pointers?
✔ Modify original values inside functions
✔ Avoid copying large structs (better performance)
✔ Share data across functions
Common Pitfalls
❌ Nil pointer dereference → Runtime panic
var ptr *int
fmt.Println(*ptr) // panic: nil pointer dereference❌ Modifying copies instead of originals
func update(x int) { x = 20 } // Doesn’t modify original
func updatePtr(x *int) { *x = 20 } // WorksFAQ
1. What is a pointer in Go?
A: A variable that stores the memory address of another variable.
var x int = 10
ptr := &x // ptr holds the address of x2. Why use pointers?
✔ Modify original values inside functions
✔ Avoid copying large structs (better performance)
✔ Share data across functions
3. What’s the zero value of a pointer?
A: nil (uninitialized pointers point to nothing).
4. How to check for nil pointers?
if ptr != nil {
fmt.Println(*ptr) // Safe dereference
}5. What’s the difference between *T and &T?
*T: Type declaration (pointer to typeT).&T: Operator to get the address of a variable.
6. Can you have a pointer to a pointer?
A: Yes (double pointers):
var x int = 10
ptr := &x
pptr := &ptr // **int
fmt.Println(**pptr) // 107. When to use pointers vs values in function arguments?
- Pointers: To modify the original value or for large structs.
- Values: For small, immutable data or read-only operations.
8. What happens if you dereference a nil pointer?
A: Runtime panic:
var ptr *int
fmt.Println(*ptr) // panic: nil pointer dereference9. How do pointers work with slices and maps?
- Slices/maps are already reference-like (no need for pointers usually).
- But pointers are needed if you want to modify the slice/map header itself.
10. What’s the output of this code?
func update(x *int) { *x = 20 }
func main() {
x := 10
update(&x)
fmt.Println(x) // ?
}A: 20 (pointer modifies the original value).
Key Points to Remember
&→ Address-of operator.*→ Dereference operator (or pointer type declaration).- Rule:
*Tis a pointer to typeT. - Always check for
nilbefore dereferencing!