Reference Types in Go
Go reference types - pointers, slices, functions, channels, maps
Reference Types
5 types
- pointers
- slices
- functions
- channels
- maps
Pointers
A pointer holds the memory address of a value. &x takes the address of x,
and *p dereferences a pointer to read or write the value it points to.
i, j := 42, 2701
p := &i // p points to i
fmt.Println(*p) // read i through the pointer -> 42
*p = 21 // write i through the pointer
fmt.Println(i) // -> 21
p = &j // p now points to j
fmt.Println(*p) // -> 2701The zero value of a pointer is nil. Dereferencing a nil pointer panics.
Structs
A struct is a collection of named fields.
type Vertex struct {
X int
Y int
}
v := Vertex{1, 2}
v.X = 4 // access and assign fields with a dotStructs can be created with field names or positionally:
v1 := Vertex{1, 2} // X:1, Y:2
v2 := Vertex{X: 1} // Y defaults to 0
v3 := Vertex{} // X:0, Y:0
ptr := &Vertex{1, 2} // a *VertexPointers to structs auto-dereference for field access: vp.X is shorthand
for (*vp).X, so no explicit dereference is needed.
vp := &v
vp.X = 1e9 // same as (*vp).X = 1e9, mutates vArrays
An array has a fixed length that is part of its type, and it is a value type (copying an array copies all its elements).
var a [2]string
a[0] = "Hello"
a[1] = "World"
primes := [8]int{2, 3, 5, 7, 11, 13, 17, 19}Slices
A slice is a view into an underlying array. It is a reference type: it is
described by a pointer to the first element, a len, and a cap.
primes := [8]int{2, 3, 5, 7, 11, 13, 17, 19}
s := primes[1:4] // [3 5 7], low inclusive, high exclusivelen= number of elements in the slice.cap= number of elements from the slice's start to the end of the underlying array, so it depends on where the slice starts.- the pointer points at the slice's first element.
names := [4]string{"John", "Paul", "George", "Ringo"}
a1 := names[0:2] // len 2, cap 4, points at "John"
a2 := names[1:3] // len 2, cap 3, points at "Paul"Because slices share the underlying array, writing through one slice changes the array and any other slice that overlaps it:
a2[0] = "ZZZ"
fmt.Println(a1) // [John ZZZ] <- overlaps a2
fmt.Println(names) // [John ZZZ George Ringo]You don't need an array to make a slice. A slice literal creates a hidden array behind the scenes:
s := []int{2, 3, 5, 7, 11, 13}
b := []bool{true, false, true, false}Slicing a slice re-slices the same backing array:
s := []int{2, 3, 5, 7, 11, 13}
s = s[1:4] // [3 5 7]
s = s[:2] // [3 5]
s = s[1:] // [5]The zero value of a slice is nil (length and capacity 0), and can be compared
with nil:
var z []int
fmt.Println(z, len(z), cap(z)) // [] 0 0
fmt.Println(z == nil) // truemake creates a slice (also maps and channels) with a given length and optional
capacity:
m := make([]int, 5) // len 5, cap 5
m2 := make([]int, 0, 5) // len 0, cap 5Watch out:
appendmay reuse the existing array if capacity allows, or allocate a new one otherwise. Don't assume two slices stay independent.
2D slices
A slice of slices works like a grid — useful for boards/matrices.
board := [][]string{
{"_", "_", "_"},
{"_", "_", "_"},
{"_", "_", "_"},
}
board[0][0] = "X"
board[2][2] = "O"
for i := 0; i < len(board); i++ {
fmt.Printf("%s\n", board[i])
}