Pointers

Contents

Pointers#

A pointer holds an address. Go has no pointer arithmetic; the operator uses pointers to share mutable state and to avoid copying large structs.

Address-of a local.

x := 10
p := &x                      // *int

Dereference to write.

*p = 20                      // x is now 20

Composite literals can yield pointers directly.

u := &User{}

new(T) returns a zero-valued *T.

alloc := new(User)

References#

  • Structs for the most common *T use case.

  • OOP for pointer vs value receivers.