Programming Languages - Pointers

Last Updated: 2024-01-21


A pointer holds the memory address of a value.

Smart pointers are data structures that have additional metadata and functionalities.


The type *T is a pointer to a T value. Its zero value is nil.

The & operator generates a pointer to its operand.

i := 42
p = &i

The * operator denotes the pointer's underlying value.

fmt.Println(*p) // read i through the pointer p
*p = 21         // set i through the pointer p

Unlike C, Go has no pointer arithmetic.

Pointers to structs

If p is a pointer to a struct, We can use p.X to access field X instead of (*p).X:

v := Vertex{1, 2}
p := &v
p.X = 1e9