Pointers, nil and the address of a value
A pointer holds the address of a value. &x takes the address of x, *p reads or writes the value at p, and new(T) allocates a zeroed T and returns its address. Go has no pointer arithmetic: a pointer is for sharing a value, not for walking memory.
Sharing a value
Every argument in Go is passed by value, so a function that must change its caller's variable takes a pointer. *n++ increments the value the pointer points at, and the caller sees the change. The same applies to a struct field, which is why rename can change the name of the caller's value.
package main
import "fmt"
type Config struct {
Name string
Port int
}
func increment(n *int) {
*n++
}
func rename(c *Config, name string) {
c.Name = name
}
func main() {
n := 41
increment(&n)
fmt.Println("n:", n)
c := Config{Name: "api", Port: 8080}
rename(&c, "gateway")
fmt.Println(c)
p := &c.Port
*p = 9090
fmt.Println("port through the pointer:", c.Port)
q := new(int)
*q = 7
fmt.Println("new int:", *q)
var np *int
fmt.Println("a nil pointer compares:", np == nil)
}
go run pointers.go
n: 42
{gateway 8080}
port through the pointer: 9090
new int: 7
a nil pointer compares: true
c.Name is shorthand for (*c).Name: the compiler dereferences a struct pointer for a field or a method, so the explicit star appears only for a whole value. p := &c.Port points at a field of a value that lives in main; it is valid as long as c is.
Copy or share
The program below makes the difference visible. A struct passed by value is copied, so the first increment changes a copy that is thrown away. A pointer passes the same value, so the second increment is visible to the caller.
package main
import "fmt"
type Counter struct {
n int
}
func addValue(c Counter) {
c.n++
}
func addPointer(c *Counter) {
c.n++
}
func main() {
c := Counter{}
addValue(c)
fmt.Println("after the copy:", c.n)
addPointer(&c)
fmt.Println("after the pointer:", c.n)
}
go run value.go
after the copy: 0
after the pointer: 1
For a small struct a copy is cheap and keeps the function free of side effects; for a large one it is waste, and for a value with a lock in it a copy is a bug. The lesson on methods turns this into the choice between a value and a pointer receiver, and go vet reports a lock that is copied.
The nil pointer
The zero value of a pointer is nil, and dereferencing it is a run-time panic. Go does not have null references in the sense of Java, but it does have this one, and a program checks a pointer it did not create itself.
package main
import "fmt"
func main() {
var p *int
fmt.Println("before the dereference")
fmt.Println(*p)
}
go build -trimpath -o nilptr nil.go
GOTRACEBACK=none ./nilptr 2>&1 | grep -v signal
./nilptr >/dev/null 2>&1; echo status: $?
before the dereference
panic: runtime error: invalid memory address or nil pointer dereference
status: 2
The panic is a nil pointer dereference and the process exits with status 2. A function that may return nil documents it, and the caller checks before it dereferences, which is the same discipline the errors lesson applies to nil errors.
Exercises
- Write swap(a, b *int) and call it with two variables. Which two addresses does the function receive?
- Return a pointer to a local variable from a function and use it in the caller. Why is this safe in Go and not in C?
- Change addPointer to take a value and return the modified struct. Which version would you choose for a struct of ten fields?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does &x give?
Answer
The address of x
The address-of operator yields a pointer to the variable.
-
What does *p do?
Answer
It reads or writes the value p points at
The dereference operator reaches the value behind the pointer.
-
What is the zero value of a pointer?
Answer
nil
A pointer that was never set is nil, and dereferencing it panics.
-
Does Go have pointer arithmetic?
Answer
No
The language has no pointer arithmetic; unsafe.Pointer has its own rules.
-
What does this program print?
package main import "fmt" func main() { n := 1 p := &n *p = 5 fmt.Println(n) }Answer
5
The pointer writes through to the variable.
-
What does this program print?
package main import "fmt" type T struct{ n int } func f(t T) { t.n = 9 } func main() { t := T{n: 1} f(t) fmt.Println(t.n) }Answer
1
The function receives a copy, so the caller's value is unchanged.
-
What does
new(int)return?Answer
A *int pointing at a zeroed int
new allocates a zero value of the type and returns its address.
-
What happens when a nil pointer is dereferenced?
Answer
A run-time panic
The address is invalid, so the runtime raises a nil pointer dereference panic.
-
Why does a method with a pointer receiver see changes?
Answer
It receives the address of the value, not a copy
A pointer receiver is the address of the value the caller called the method on.
-
Is a pointer to a local variable valid after the function returns?
Answer
Yes, the compiler moves the value to the heap
Escape analysis allocates values that outlive their frame on the heap, and the collector manages them.
The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev