Go Pointers Basics — Find the Bug¶
Instructions¶
Each exercise has buggy Go code. Identify the bug, explain, fix. Difficulty: 🟢 🟡 🔴.
Bug 1 🟢 — Nil Pointer Dereference¶
Solution
**Bug**: `p` is nil; `*p` panics. **Fix**: **Key lesson**: Always nil-check.Bug 2 🟢 — Forgot &¶
Solution
**Bug**: Passing `n` (int), but function expects `*int`. Compile error. **Fix**: `incr(&n)`. **Key lesson**: Use `&` to take address.Bug 3 🟢 — Forgot * to Dereference¶
Solution
**Bug**: Prints the pointer value (address), not the pointed-to value. **Fix**: `fmt.Println(*p)` for the value `5`. **Key lesson**: `*p` to read the pointee.Bug 4 🟡 — Pointer Arithmetic¶
Solution
**Bug**: Go forbids pointer arithmetic. **Compile error**: `invalid operation: p++`. **Fix**: use slice indexing: `arr[1]` or `arr[:][1]`. **Key lesson**: Use slices, not pointer arithmetic.Bug 5 🟡 — Address of Map Value¶
Solution
**Bug**: Map values are not addressable. **Compile error**. **Fix** (extract): **Key lesson**: Map values are not addressable. Use copy-modify-restore.Bug 6 🟡 — Method on Map Value¶
Solution
**Bug**: `m["a"]` is not addressable; can't call pointer-receiver method. Compile error. **Fix** — store pointers: **Key lesson**: For mutable struct values in maps, store pointers.Bug 7 🟡 — Loop Variable Pointer (Pre 1.22)¶
var ptrs []*int
for _, x := range []int{1, 2, 3} {
ptrs = append(ptrs, &x)
}
for _, p := range ptrs {
fmt.Println(*p)
}
(Go 1.21 module.) What prints?
Solution
**Pre Go 1.22**: All `&x` are the same pointer (same variable). After loop, x = 3. Prints: **Go 1.22+**: Each iteration's x is fresh. Prints `1 2 3`. **Fix for pre-1.22**: **Key lesson**: Loop variable capture; pre-1.22 needs shadow.Bug 8 🔴 — Aliasing Race¶
Solution
**Bug**: Two goroutines mutate `shared.N` without synchronization. Race. **Fix**: mutex or atomic: Or: **Key lesson**: Shared pointers across goroutines need synchronization.Bug 9 🔴 — Returning Pointer to Loop-Local¶
func makeAll() []*int {
var ptrs []*int
for i := 0; i < 3; i++ {
ptrs = append(ptrs, &i)
}
return ptrs
}
(Go 1.21.) What does each pointer point to?
Solution
**Pre 1.22**: All point to the same `i`. After loop, i = 3. All return 3. **Go 1.22+**: Each iteration's `i` is distinct. Pointers point to 0, 1, 2. **Fix for pre-1.22**: **Key lesson**: Test with both Go versions if your module supports a range.Bug 10 🔴 — Pointer Receiver, Value Used¶
type T struct{ n int }
func (t *T) Inc() { t.n++ }
func main() {
t := T{n: 1}
t.Inc()
fmt.Println(t.n)
}
Does this work?