sajad torkamani

What is a pointer?

A pointer is a special variable that holds the memory address of another variable.

Initialise a pointer

var myPointer *int

myPointer is initialised to the zero value of nil.

Access memory address of a variable

var num int = 5

fmt.Println(num) // prints 5

fmt.Println(&num) // 0xc000018030

Generate a pointer to a variable

myInt := 42
myPointer = &myInt

myPointer holds the memory address of myInt.

Get a pointer’s value (dereference)

fmt.Println(*myPointer)

This reads the value at the pointer myPointer

Tagged: Golang