Essential Go  Suggest an edit


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 to or subtract from pointers.

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: 0xc00007c008
v: 4
pv: 0xc00007c008, pv2: 0xc00007c008

  ↑ ↓ to navigate     ↵ to select     Esc to close