typestar

Arrays are values in Go

An array has a fixed length in its type, and assigning one copies it.

func arrays() (int, int, bool) {
    original := [4]int{1, 2, 3, 4}
    copied := original
    copied[0] = 99

    grid := [2][3]int{{1, 2, 3}, {4, 5, 6}}
    inferred := [...]string{"a", "b", "c"}

    return original[0], grid[1][2], len(inferred) == 3
}

How it works

  1. [3]int and [4]int are different types.
  2. Assignment and passing copy the whole array.
  3. That is why slices exist, and why arrays are rare.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
231
Tokens
96
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 64 seconds.

Type this snippet

Step 1 of 9 in Arrays, slices & maps, step 16 of 30 in Language basics.

← Previous Next →