Go Reference ยท Operators
The & and * operators
Reference for Go's & (address-of) and * (dereference, pointer type) operators: taking addresses, composite literals, nil pointers and what is addressable.
The unary & operator produces a pointer to its operand. The unary * operator, applied to a pointer, gives the value it points to (dereferencing), and can also appear on the left side of an assignment to change that value. In a type, *T means "pointer to T".
Pointers let functions modify their arguments, share large values without copying, and represent "no value" with nil. Go has no pointer arithmetic, and the garbage collector keeps pointed-to values alive, so taking the address of a local variable is safe.
p := &x // p has type *int *p = 42 // changes x
Try it
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.
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
&operand // address-of; yields *T *pointer // dereference; yields T *T // pointer type
| Name | Description |
|---|---|
operand |
Must be addressable (a variable, pointer indirection, slice element, field of an addressable struct, element of an addressable array) or a composite literal. |
pointer |
A value of type *T. Dereferencing a nil pointer panics at run time. |
Edge cases
- You cannot take the address of a map element (
&m["k"]), a constant, a function call result, or a string index: none are addressable.&Point{}works because composite literals are a special case. *nilPointercauses a panic with "invalid memory address or nil pointer dereference".- Selector expressions dereference automatically:
p.fieldandp.Method()work on a*Twithout writing(*p). - Pointers to arrays can be indexed and sliced directly:
pa[0],pa[1:],len(pa).
Usage notes
- Pointers compare with
==and!=. Two pointers are equal if they point to the same variable or both are nil. - The address of a variable can outlive its function. The compiler moves such variables to the heap (escape analysis), so returning
&localis fine. - Slices, maps, channels and functions are already reference-like; you rarely need a pointer to them.
- Use a pointer receiver or parameter when the function must modify the value or the struct is large. For small immutable values, plain values are simpler and often faster.
new(T)is equivalent to declaring a zeroTvariable and taking its address. For structs,&T{...}is more common.*is also the multiplication operator in binary position; the compiler tells them apart by context.- Pointers are not nil-safe by default. Check before dereferencing values that can be absent.
- Since Go 1.26,
newaccepts an expression:new(42)ornew(time.Second)allocates a pointer initialized to that value, handy for optional struct fields.
Related
- = and op=, Pointers, Indexing and slicing
- new() and make()
- Tutorial: Pointers, Pointers and functions, make and new