Pointers in Go: Memory Addresses Without the Headache
What & and * actually do in Go, when Go copies values vs shares memory addresses, and how to avoid nil-pointer panics.
You'll learn what a pointer actually stores, how & (address-of) and * (dereference) work, and when to pass a pointer vs a value in Go. Assumes familiarity with Go variables and structs.
1. Go copies everything by default
When you pass a variable into a Go function, Go makes a photocopy of it. If the function scribbles on the photocopy, your original paper on the desk stays untouched:
func giveRaise(salary int) {
salary += 10000
}
func main() {
mySalary := 50000
giveRaise(mySalary)
fmt.Println(mySalary) // still 50000. Tragedy.
}To let a function modify your original variable, you don't hand it a photocopy—you hand it the street address of where that variable lives in memory. That street address is a pointer.
2. The two symbols: & and *
Only two operators matter:
&x(address-of): "Give me the memory address wherexlives."*p(dereference): "Follow the addresspand read/write the actual value sitting there."
package main
import "fmt"
func actuallyGiveRaise(salary *int) {
*salary += 10000
}
func main() {
mySalary := 50000
actuallyGiveRaise(&mySalary)
fmt.Println(mySalary)
}60000Look at the types:
mySalaryhas typeint.&mySalaryhas type*int("pointer to anint").- Inside
actuallyGiveRaise,*salary += 10000follows the address back tomySalaryand adds10000directly to the original value.
Unlike C, Go forbids pointer arithmetic (p + 1), which means you cannot accidentally walk off the end of memory and corrupt random bytes.
Log fmt.Println(&mySalary) in main() to see an actual hexadecimal memory address like 0xc0000140a8.
3. Slices and maps already act like pointers (mostly)
Wait—why didn't we need * to change items in a slice or a map? Because a slice header and a map already hold an internal pointer to their backing data. Passing scores (a map[string]int) into a function lets that function add/update keys on the same map without *map.
4. Guarding against nil pointers
An uninitialized pointer (var p *int) holds nil—it points at nowhere. If you try to read or write *p when p is nil, Go panics immediately:
panic: runtime error: invalid memory address or nil pointer dereferenceWhenever a pointer might legitimately be absent (like an optional config field), check if p != nil before dereferencing *p.
Common mistakes
| Error | What it means | Fix |
|---|---|---|
invalid memory address or nil pointer dereference | Dereferenced a nil pointer | Check if p != nil or initialize with &val / new(T) |
cannot use x (type int) as type *int | Forgot & when calling a pointer function | Pass &x |
| Using pointers for tiny ints/strings needlessly | Adds nil checks and heap allocations | Pass small values by value unless you need mutation or optional nil |
Practice
- If
a := 5,p := &a, and*p = 99, what doesfmt.Println(a)print?
Answer: 99, because p points to a's memory location.
What you learned
- Go passes copies by default;
&xgets a variable's address and*paccesses the value at that address. - Always check
if p != nilbefore dereferencing an optional pointer. - Pass small values by value unless you need to mutate the original or represent an optional
nilstate.
Video walkthrough
YouTubeClick play to load video (YouTube)