Essential Go Arrays  Edit on GitHub      File Issue

Array indexes

Array values should be accessed using a number specifying the location of the desired value in the array. This number is called the Index.

Indexes start at 0 and finish at array length -1.

To access a value, you have to do something like this: arrayName[index], replacing “index” by the number corresponding to the offset of the value within your array.

For example:

var array = [6]int{1, 2, 3, 4, 5, 6}

// doesn't work: invalid array index -1 (index must be non-negative)
// fmt.Println(array[-42])
fmt.Println(array[0]) // > 1
fmt.Println(array[1]) // > 2
fmt.Println(array[2]) // > 3
fmt.Println(array[3]) // > 4
fmt.Println(array[4]) // > 5
fmt.Println(array[5]) // > 6
// doesn't work: invalid array index 6 (out of bounds for 6-element array)
//fmt.Println(array[6])
1
2
3
4
5
6

To set or modify a value in the array, you use the same index-based method. Example:

var array = [6]int{1, 2, 3, 4, 5, 6}

fmt.Println(array) // > [1 2 3 4 5 6]

array[0] = 6
fmt.Println(array) // > [6 2 3 4 5 6]

array[1] = 5
fmt.Println(array) // > [6 5 3 4 5 6]

array[2] = 4
fmt.Println(array) // > [6 5 4 4 5 6]

array[3] = 3
fmt.Println(array) // > [6 5 4 3 5 6]

array[4] = 2
fmt.Println(array) // > [6 5 4 3 2 6]

array[5] = 1
fmt.Println(array) // > [6 5 4 3 2 1]
[1 2 3 4 5 6]
[6 2 3 4 5 6]
[6 5 3 4 5 6]
[6 5 4 4 5 6]
[6 5 4 3 5 6]
[6 5 4 3 2 6]
[6 5 4 3 2 1]
  ↑ ↓ to navigate     ↵ to select     Esc to close