• Pointer holds memory address of value
  • *Tis a pointer to a Tvalue
i := 42
 
// Create a pointer, not assigned to anything. It's nil
var p *int
 
// Assign the address of i to the pointer
p = &i
 
fmt.Println(p) // prints the memory address 

The *operator itself denotes the pointer’s underlying value. Therefore, while pin the example is the memory address, *preturns the value “behind” the memory address.

When to use pointers

There are mainly two use-cases for pointers:

  • If you need direct access to the value
  • If copying a value to a function or method is too expensive, e. g. for huge strings. Then using a pointer for passing the value will make the function much faster

Next chapter: Structs