Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Array assignment (pointer)

  • &
package main

import (
	"fmt"
)

func main() {
	var ar = [3]int{1, 2, 3}
	br := &ar

	fmt.Println(ar)
	fmt.Println(br)
	ar[1] = 42
	fmt.Println(ar)
	fmt.Println(br)

	fmt.Println(ar[1])
	fmt.Println(br[1])

}
[1 2 3]
&[1 2 3]
[1 42 3]
&[1 42 3]
42
42

This way br is a poiner to ar so they refer to the same data in memory. Even though br is a pointer we can still access elements in the same way as in the original array. We'll discuss pointers in depth later on.