Golang Pointers in 5 Minutes
Go pointers for beginners in under five minutes, on a real Go program: why a function that changes a struct leaves the caller's copy untouched, what *T and & mean, how a pointer receiver lets rex.Walk() change rex, why a missing map entry gives a nil pointer and a panic, and why returning the address of a local variable is fine in Go.
Two walks, zero counted
You pass a struct to a Go function, the function changes it, and back in main, nothing changed. Trish runs a dog-walking business. Her dog Rex went out twice, so addWalk ran twice, and her program prints: Rex walks, zero. In under five minutes, you'll fix it with two small symbols, and see the pointer mistake that crashes Go programs.
sed -n '10,19p' main.gofunc addWalk(d Dog) { d.Walks++ } func main() { rex := Dog{Name: "Rex"} addWalk(rex) addWalk(rex) fmt.Println(rex.Name, "walks:", rex.Walks) }
go run .Rex walks: 0
Go hands over a copy
Here's why. In Go, every argument is copied. Call addWalk with rex, and the function gets its own copy of the whole dog. It adds one to the copy, then the copy is thrown away. The second call: a fresh copy, the same. The rex in main was never touched.

You can see the copies. Inside addWalk, the dog is called d. Trish prints where each one lives in memory, its address. Guess first: same address, or different? Different, and new on each call: a fresh dog for every call, while Rex stays at zero.
go run .main's rex: 0x42a7c3c2c030 addWalk's d: 0x42a7c3c2c048 walks 1 addWalk's d: 0x42a7c3c2c060 walks 1 Rex walks: 0
Think of hiring a painter. Hand them a photo of your house, and they paint the photo. Your house stays the same color. Hand them your street address, and they go to the real house. A pointer is that address.

The fix: pass the address
So Trish passes the address. In the function, the type becomes *Dog: a star before a type means a pointer to a Dog. In main, she passes &rex: an ampersand before a variable gives its address. The line that printed d's address loses its ampersand too, because d is already an address.

Run it again. Now addWalk's d has the same address as main's rex, on both calls. The walks go to one, then two, and Rex walks: two.
go run .main's rex: 0x4ad5c9ad0048 addWalk's d: 0x4ad5c9ad0048 walks 1 addWalk's d: 0x4ad5c9ad0048 walks 2 Rex walks: 2
& and * on their own
Forget the dog for a moment: here's a plain number called walks, set to five. p is ampersand walks: its address. Then: star p equals six. A star before a pointer means go to that address, so walks is now six. A star before a type, like star int, means a pointer to an int, and one that points at nothing yet is nil.
sed -n '6,12p' main.gowalks := 5 p := &walks fmt.Println(p) *p = 6 fmt.Println(walks) var q *int fmt.Println(q)
go run .0x3dda64c1c008 6 <nil>
Methods take the address for you
Real Go code rarely passes ampersands by hand. The star goes on a method instead: a function attached to a type. The bit in brackets before the name, d *Dog, is called the receiver, and here it's a pointer. Inside, d.Walks needs no star: Go follows the pointer for a dot by itself. Trish writes rex dot Walk on a plain Dog, and Go takes the address for her: two walks.
sed -n '10,18p' main.gofunc (d *Dog) Walk() { d.Walks++ } func main() { rex := Dog{Name: "Rex"} rex.Walk() rex.Walk() fmt.Println(rex.Name, "walks:", rex.Walks)
go run .Rex walks: 2
Effective Go, the Go team's own guide, says it plainly: if the value lives in a variable, Go adds the ampersand for you. A method that changes its value gets a star receiver: the pointer you'll write most.

When a pointer is nil
Here's the crash. Trish keeps her dogs in a map of pointers, looked up by name. Her helper Mike books a walk for Max, who isn't in it. Guess first: what happens? It crashes. A missing key hands back nil, an address to nowhere, and Walk follows it to no dog: nil pointer dereference.
sed -n '15,17p' main.godogs := map[string]*Dog{"Rex": {Name: "Rex"}, "Bella": {Name: "Bella"}} dogs["Rex"].Walk() dogs["Max"].Walk()
go run .panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa2198] goroutine 1 [running]: main.(*Dog).Walk(...) /home/trish/walks/main.go:11 main.main() /home/trish/walks/main.go:17 +0x178 exit status 2
The fix: check before you follow it. Trish asks the map for two things: the dog, and a true or false called ok, which says whether Max is there at all. If not, she says so and stops. A pointer you got any other way: compare it with nil first.
sed -n '16,21p' main.god, ok := dogs["Max"] if !ok { fmt.Println("no dog called Max") return } d.Walk()
go run .no dog called Max
Returning a pointer
When a function ends, its variables are cleared away. So what if it hands back the address of one? NewDog does exactly that. Go's compiler notices, and moves d to the heap, memory that stays around after the function ends. Build with a flag that makes the compiler explain itself, and it prints: moved to heap, d. Max keeps his walk, and that's the New function you'll see in Go packages everywhere.
sed -n '10,19p' main.gofunc NewDog(name string) *Dog { d := Dog{Name: name} return &d } func main() { max := NewDog("Max") max.Walks++ fmt.Println(max.Name, "walks:", max.Walks) }
go build -gcflags=-m . 2>&1 | grep moved./main.go:11:2: moved to heap: d
go run .Max walks: 1
So when do you use one? When the value must change. When a struct is big and copying it is waste. When nil means something, like no dog found. A small value you only read? Just copy it.

Recap
So: Go copies every argument. A star type holds an address, an ampersand takes one, and a pointer can be nil, so check it. Give the painter the address, not the photo.

If pointers make sense now, a like helps YouTube show this to more beginners, and subscribing brings the next Go idiom: error return values.
Every command here ran for real, and the lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
- The Go Programming Language Specification: Calls (arguments passed by value), Address operators, Method calls (x.m() is shorthand for (&x).m()), The zero value (nil pointers)
- Effective Go: Pointers vs. Values, and Composite literals (returning the address of a local variable)
- Go FAQ: When are function parameters passed by value? and stack or heap allocation
- A Tour of Go, Pointers
Demos: one container on our own Mac standing in for a laptop; the dog-walking business is made up. Every command, error and number was real. Watch the full video on YouTube.