Pointers
Like C, go uses pointers. A pointer is used to refer to the address in memory of a variable. You can then use the pointer to refer back to the variable.
package main
import (
"fmt"
)
func main() {
x := 1
p := &x
fmt.Println(p) // prints the address
fmt.Println(*p) // prints the value of what's at that address (1)
*p = 2 // updates x via the pointer
fmt.Println(x)
}
/*
Prints the following:
0xc0000b2008
1
2
*/
The &
operator returns a variable’s address, a pointer to the variable.
The *
operator is used on a pointer to return the value at that address. This is called dereferencing.
The value of a variable that is a pointer is a memory address, something like 0xc0000b2008
.
It’s quite common to pass around pointers as arguments to functions, and use this as a method for aggregating values between functions.
If you want a function to mutate a variable, then you write it to accept a pointer type. For example.
// double the value of an integer
func double(i *int) {
*i += *i
}
TLDR
When you see &
in front of a variable, read it as “address of”.
When you see *
in front of a variable read it as “value at address”.