Essential Go  Edit on GitHub      File Issue


Go has pointers, like C++. A pointer to a variable is the address of that variable in memory.

Unlike C++, Go doesn’t allow pointer arithmetic. You can’t add or substract from pointers.

A zero value of a pointer is nil.

Basics of pointers:

v := 5

// pv is a pointer to v
pv := &v
fmt.Printf("v: %d, pv: %p\n", v, pv)

// we change the value of v via pv
*pv = 4
fmt.Printf("v: %d\n", v)

// two pointers to the same value have the same address
pv2 := &v
fmt.Printf("pv: %p, pv2: %p\n", pv, pv2)
v: 5, pv: 0xc420016090
v: 4
pv: 0xc420016090, pv2: 0xc420016090
  ↑ ↓ to navigate     ↵ to select     Esc to close