Frontier Software

Types

pointers

Ampersands and Asterisks

Go uses the same notation as C for pointers where the memory address of a variable x is &x and if we store this memory address in p then whatever is stored in that memory address is retrieved as *p.

The unary operator & gives the address of an object

/*
Simple use of the & unary operator
*/
package main

import "fmt"

func main() {
	s := "I know your address"
	fmt.Printf("%v\n", &s)
}

Running that gives me 0xc000014080. This memory address is a value that can be stored in a variable to get used (or abused) in various ways. A variable containing a memory address is called a pointer.