Go Pointers Explained Simply

Updated 4 min read
Go Pointers featured image: cards showing the & and * symbols and a nil pointer

Go pointers let you share a value instead of copying it, so a function can change the original data. In this post, you will learn the two pointer symbols, when Go copies data and when it shares it. Each idea comes with a short example that you can run yourself.

You already met pointers briefly in structs and methods in Go, where a pointer receiver changed a bank balance. Now we will look at them properly. You will also pass pointers into functions, so a quick look at writing and calling functions in Go will help.

What Are Go Pointers?

Every variable lives somewhere in your computer’s memory. That location has an address, much like a house has a street address. A pointer is simply a variable that stores this address.

Think of a shared document link. Instead of emailing a copy of a file, you send the link, and everyone edits the same file. Go pointers work the same way, because they point to one value that many parts of your code can reach.

The Two Pointer Symbols: & and *

Getting an Address with &

age := 30
p := &age

fmt.Printf("%T %T\n", age, p) // int *int
fmt.Println(p != nil)          // true

The & sign gives you the address of a variable. So p now holds where age lives. Its type is *int, which you read as “pointer to int”.

Reading and Changing with *

fmt.Println(*p) // 30

*p = 31
fmt.Println(age) // 31

Putting * before a pointer follows the address to the real value. This step is called dereferencing. As a result, *p = 31 changes age itself, even though we never wrote age on that line.

Why Go Pointers Matter: Copy vs Share

By default, Go passes a copy of every value into a function. So the function can change its copy, but the original stays the same. A pointer changes that rule, as the diagram below shows.

Go Pointers diagram showing a copy passed to a function next to a pointer that changes the original

A plain value goes in as a copy, while a pointer lets the function reach and change the original x.

Passing a Pointer to a Function

func double(n int) {
	n = n * 2
}

func doublePtr(n *int) {
	*n = *n * 2
}

x := 5
double(x)
fmt.Println(x) // 5

doublePtr(&x)
fmt.Println(x) // 10

The first function doubles a copy, so x stays 5. The second receives the address with &x, and then it changes the real value. In short, pass a pointer whenever a function must update your variable.

Go Pointers to Structs

type User struct {
	Name string
}

func rename(u *User) {
	u.Name = "Ravi"
}

u := User{Name: "Asha"}
rename(&u)
fmt.Println(u.Name) // Ravi

With structs, Go lets you write u.Name instead of (*u).Name. Also, passing a pointer avoids copying a large struct, which saves memory.

Nil Pointers and new

var q *int
fmt.Println(q == nil, q) // true <nil>

n := new(int)
*n = 7
fmt.Println(*n) // 7

Go pointers that point nowhere have the zero value nil. Meanwhile, the built-in new function creates a zero value and returns its address. You will use it rarely, since &Type{} is more common.

Go also lets a function return the address of a local variable. For example, return &c is completely safe, because Go keeps that value alive for as long as something points to it.

When to Use Go Pointers

Use a pointer whenUse a plain value when
A function must change the originalThe function only reads data
The struct is largeThe type is small, like int or bool
“No value” must be possible (nil)A zero value is fine

Slices and maps already share their data behind the scenes. So you rarely need a pointer to a slice or a map, as you saw in slices and arrays in Go.

Common Mistakes with Go Pointers

Error you seeWhy it happensHow to fix it
panic: runtime error: invalid memory address or nil pointer dereferenceThe code used *p while p was nil.Check p != nil first.
Change has no effectThe function got a copy.Pass &x and take *int.
cannot use x (variable of type int) as *int valueThe & sign is missing.Call doublePtr(&x).

The nil pointer panic is the most common crash in Go programs. Pointer questions also show up often in my Go interview questions and answers, so practise them there.

Official Resources

These pages come straight from the Go team, so they are always accurate and up to date.

Quick Recap

  • Go pointers store the address of a value instead of the value itself.
  • Use & to get an address and * to read or change the value.
  • Go passes copies by default, so pass a pointer to change the original.
  • A pointer with no target is nil, so check it before you use it.
  • Slices and maps already share data, so they rarely need pointers.

What’s Next?

Now you know how Go shares data. Next, we will learn interfaces, which let different types share the same behaviour.

Coming soon: Go Interfaces Explained with Examples