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 (copy)

package main

import (
	"fmt"
)

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

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

Assigning an array is by default creates a copy. Some cases this is what we want, in other cases we'd prefer to have reference / pointer to the same data in memmory.