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
[3]intand[4]intare different types.- Assignment and passing copy the whole array.
- That is why slices exist, and why arrays are rare.
Keywords and builtins used here
boolfuncintlenreturnstring
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.
Step 1 of 9 in Arrays, slices & maps, step 16 of 30 in Language basics.