Go Arrays
An array is a numbered sequence of elements of the same type with a fixed length. The length is part of the type itself: [3]int and [4]int are two different, incompatible types. Arrays are the building block underneath slices, which you will use far more often — but understanding arrays first makes slices much easier.
Declaring arrays
package main
import "fmt"
func main() {
var scores [3]int // three zeros
days := [3]string{"Mon", "Tue", "Wed"} // array literal
primes := [...]int{2, 3, 5, 7, 11} // ... lets the compiler count
fmt.Println(scores, len(scores))
fmt.Println(days)
fmt.Println(primes, len(primes))
}[0 0 0] 3
[Mon Tue Wed]
[2 3 5 7 11] 5
| Syntax | Meaning |
|---|---|
var a [5]int |
five elements, all set to the zero value |
a := [3]int{1, 2, 3} |
length 3, every element given |
a := [5]int{1, 2} |
length 5; the rest are zero: [1 2 0 0 0] |
a := [...]int{4, 5, 6} |
length counted from the values (3) |
a := [5]int{1: 10, 3: 30} |
set specific indexes: [0 10 0 30 0] |
Accessing and changing elements
Indexes start at 0. The last element is at len(a)-1.
package main
import "fmt"
func main() {
temps := [5]float64{18.5, 21.0, 19.2, 23.4, 20.1}
fmt.Println("First:", temps[0])
fmt.Println("Last:", temps[len(temps)-1])
temps[2] = 25.0
fmt.Println(temps)
sparse := [6]string{1: "b", 4: "e"}
fmt.Printf("%q\n", sparse)
}First: 18.5
Last: 20.1
[18.5 21 25 23.4 20.1]
["" "b" "" "" "e" ""]
Using an index that is out of range with a constant index is a compile error; with a variable index it is a runtime panic (index out of range).
Looping over an array
package main
import "fmt"
func main() {
sales := [4]int{120, 95, 143, 88}
total := 0
for i := 0; i < len(sales); i++ {
total += sales[i]
}
fmt.Println("Total:", total)
for i, v := range sales {
fmt.Printf("Q%d: %d\n", i+1, v)
}
}Total: 446
Q1: 120
Q2: 95
Q3: 143
Q4: 88
Arrays are values
This is the big difference from many other languages: assigning an array or passing it to a function copies all of its elements. Changing the copy does not affect the original.
package main
import "fmt"
func double(nums [3]int) {
for i := range nums {
nums[i] *= 2
}
fmt.Println("inside: ", nums)
}
func main() {
original := [3]int{1, 2, 3}
copyOf := original
copyOf[0] = 100
double(original)
fmt.Println("original:", original)
fmt.Println("copy: ", copyOf)
fmt.Println(original == [3]int{1, 2, 3}) // arrays can be compared
}inside: [2 4 6]
original: [1 2 3]
copy: [100 2 3]
true
Arrays of the same type can be compared with == — it is true when every element is equal.
Multi-dimensional arrays
An array can hold other arrays, which gives you grids and matrices.
package main
import "fmt"
func main() {
board := [3][3]string{
{"X", "O", "X"},
{" ", "X", "O"},
{"O", " ", "X"},
}
for _, row := range board {
fmt.Println(row)
}
fmt.Println("Centre:", board[1][1])
}[X O X]
[ X O]
[O X]
Centre: X
Tip: Because their size is fixed, arrays are mostly used for data with a natural fixed size — RGB colours (
[3]uint8), a chess board, a SHA-256 hash ([32]byte). For lists that grow or shrink, use a slice (next chapter).
Test yourself
What is the length of a := [...]string{"x", "y", "z"}?
- 0
- 3
- It grows dynamically
- Compile error
What happens to the original array when you pass it to a function that modifies its parameter?
- It changes too
- It is unchanged, because arrays are copied
- The program panics
- It becomes nil
What does var grid [2]int contain?
[][nil nil][0 0]- Random values