Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Built-in Types

Go pointers

Reference for Go pointers: the *T type, & and * operators, new, nil pointers, pointers to structs, automatic dereferencing and what Go does not allow.

A pointer holds the memory address of a value. The type *T is "pointer to T". You get an address with &x and follow it with *p. Go passes everything by value, so pointers are how a function modifies its caller's data or avoids copying a large struct.

Unlike C, Go has no pointer arithmetic, and the garbage collector keeps a value alive as long as any pointer refers to it, so returning the address of a local variable is safe. The zero value of a pointer is nil.

Go
n := 5
p := &n
*p = 6 // n is now 6

Try it

Go Address-of and dereference

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Struct pointers and nil

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
var p *T          // nil pointer to T
p = &x            // address of an addressable variable x
p = &T{...}       // address of a composite literal
p = new(T)        // pointer to a zero T
v := *p           // dereference (copy the value)
*p = v            // store through the pointer
Part Description
&x Address of x. x must be addressable: a variable, a field of an addressable struct, a slice element, or a composite literal. Map elements and function results are not addressable.
*p The value p points to. Panics if p is nil.
p.f Shorthand for (*p).f when p is a pointer to a struct. Methods are called the same way.

Edge cases: comparing pointers with == tests identity (same address). Two pointers to distinct zero-size values may or may not be equal. There is no p++ or p + 1; use slices or the unsafe package for low-level work.

Usage notes

  • Use a pointer receiver when a method must change the receiver, when the struct is large, or when it contains a lock. Be consistent: mix of value and pointer receivers on one type is a lint warning.
  • Do not pass pointers to small values for speed. Copying an int or a string header is cheaper than an indirection and may avoid a heap allocation.
  • Pointers to slices and maps are rarely needed. Slices and maps are already small headers that reference shared data; pass a *[]T only when the callee must change the slice's length.
  • Optional values. A *int can express "absent" as nil, common in JSON structs, at the cost of an allocation and nil checks.
  • Loop variables. Before Go 1.22, &v for a for loop variable pointed at one shared variable. Since Go 1.22 each iteration has its own copy.
  • nil receivers. A method on a pointer type may be called with a nil pointer and may handle it; a plain field access on nil panics.
  • Escape analysis. The compiler decides whether a value lives on the stack or the heap; taking an address does not by itself force a heap allocation.