Updated 16 Nov 2026: rewritten. The first example in the original declared a pointer it never used, so it wouldn’t compile, and it described slices and maps as “passed by reference”. This version has working examples and states the by-value rule precisely.
A pointer is a variable that holds the memory address of another variable. Pointers let a function change its caller’s data and let you avoid copying large values. Go keeps them simple: you can take an address and follow it, but there’s no pointer arithmetic.
The two operators #
&xgives the address ofx.*pfollows the pointerpand gives the value stored at that address. This is called dereferencing.
package main
import "fmt"
func main() {
x := 10
p := &x // p is a pointer to x, of type *int
fmt.Println(x) // 10
fmt.Println(*p) // 10: the value p points to
*p = 20 // change x through the pointer
fmt.Println(x) // 20
fmt.Printf("%T\n", p) // *int
}10
10
20
*intThe type *int means “pointer to an int”. Printing p itself shows an address such as 0xc000012080. It differs from run to run, so it’s rarely useful on its own.
Why functions need pointers #
Go passes every argument by value: the function gets a copy. So a function that changes its parameter changes only the copy:
func increment(a int) {
a++
}
func main() {
x := 10
increment(x)
fmt.Println(x) // 10: unchanged
}To let the function change x, pass its address and have the function dereference it:
func increment(a *int) {
*a++ // same as (*a)++
}
func main() {
x := 10
increment(&x)
fmt.Println(x) // 11
}increment still receives a copy, but a copy of the address, and both addresses refer to the same x. That’s why people say Go has no pass by reference: it only ever passes values, and a pointer is a value that happens to be an address.
Pointers to structs #
Pointers to structs are the most common use. Go lets you write p.Name instead of (*p).Name:
type Person struct {
Name string
Age int
}
p := &Person{Name: "Ada", Age: 36}
p.Age++ // Go dereferences p for you
fmt.Println(p.Name, p.Age) // Ada 37Creating pointers #
Besides &x on an existing variable, you can take the address of a composite literal, as &Person{...} above does. The built-in new(T) allocates a zero value of type T and returns a pointer to it:
n := new(int)
fmt.Println(*n) // 0
*n = 5
fmt.Println(*n) // 5Unlike C, it’s safe to return a pointer to a local variable. Go notices that the pointer outlives the function and keeps the variable alive:
func newCounter() *int {
c := 0
return &c
}nil pointers #
A pointer that doesn’t point to anything is nil, and that is also the zero value of every pointer type. Dereferencing it makes the program panic:
var p *int
fmt.Println(p == nil) // true
fmt.Println(*p) // panic: runtime error: invalid memory address or nil pointer dereferenceSo check for nil before you use a pointer you didn’t create yourself, especially one returned from a function that can fail.
When to use a pointer #
Use a pointer when:
- a function or method needs to modify its argument or receiver,
- the value is large, and copying it on each call would be wasteful,
- you need an optional value, where
nilmeans “not set”.
For small values that aren’t modified, such as numbers, strings and small structs, pass them by value. It’s simpler, and the values can’t be changed unexpectedly by other code.
Slices, maps and pointers #
A common mix-up: slices and maps seem to be passed by reference, because a function can change their contents without a pointer. They’re still passed by value. The thing copied is a small header that points to shared data, so the copy and the original see the same elements. What the function can’t do is change the caller’s slice length by appending, because that changes the copy of the header. This is why functions that append return the new slice. The slices post covers it.
Go is garbage collected, so you never free memory yourself. Next: structs, methods and interfaces.