A pointer in Go is a variable that holds the memory address of another value. You take an address with &, read or write through it with *, and a pointer type is written *T. Pointers let functions modify the caller's data and avoid copying large structs, but Go does not allow pointer arithmetic, and the garbage collector manages the memory they point to.
This guide covers the syntax, passing pointers to functions, pointer receivers, nil pointers and the common pitfalls. Examples target current Go (1.22 and later).
The zero value of any pointer is nil. The two operators are inverses of each other:
&x gives the address of x. *p follows the pointer to the value. Changing *p changes x because both refer to the same memory.
Go always passes arguments by value, so a function receives a copy. For a pointer, the copy is of the address, which still points to the original data.
Use a pointer parameter when the function must modify the caller's variable, or when copying a big struct on every call would be wasteful. For small values such as ints and short structs, passing by value is usually as fast and simpler to reason about.
For a pointer to a struct, you access fields directly. Go inserts the dereference for you, so p.Name means (*p).Name.
A method with a pointer receiver can modify the value it is called on. A value receiver works on a copy.
If any method of a type needs a pointer receiver, make all of them pointer receivers for consistency. This also matters for interfaces: a *Counter has both methods in its method set, but a plain Counter value has only Value.
Dereferencing a nil pointer causes a runtime panic. Check before use when a pointer can be absent.
new(T) allocates a zeroed T and returns a *T. It is rarely needed because &T{} does the same and also lets you set fields. Go has no free: when nothing references the value any more, the garbage collector reclaims it.
It is safe to return a pointer to a local variable. The compiler's escape analysis moves the value to the heap when needed:
&v of a range variable pointed to the same variable every time.*string distinguishes missing from empty), though generics and sql.Null types are alternatives.unsafe package allows pointer arithmetic, but it bypasses type safety and should be limited to low-level code.No. You cannot add to a pointer or index through it in safe code. Use slices to walk through memory instead.
Pass by pointer when the function mutates the struct or the struct is large. Otherwise a value is fine and avoids nil checks and shared mutation.
new(T) returns a *T to a zeroed value of any type. make creates slices, maps and channels and returns the value itself, not a pointer.
Yes. Two pointers are equal if they point to the same variable or are both nil. That is identity, not equality of the pointed-to values.
Pointers store addresses: & takes one, * follows one, and nil means no target. Use them to mutate arguments, to share large values and for pointer receivers, and always guard against nil. Go keeps pointers safe by forbidding arithmetic and managing memory with the garbage collector.