Go Arrays

Summary: in this tutorial, you will learn how to use the Go arrays to store a fixed number of elements of the same type.

In Go, arrays are a basic data structure for storing a fixed number of elements with the same types. For example, you can use arrays to store a list of numbers or strings.

Declaration

To declare an array, you specify the size and the type of element as follows:

var arrayName [size] typeCode language: Go (go)

For example, the following declares an array of five integers:

var scores [5] intCode language: Go (go)

This declaration creates an array of 5 integers. Since the default values of integers are zero, the scores array holds five numbers zero.

Initialization

Like other variables, Go allows you to initialize an array at the time of declaration. For example:

var scores [5] int = [5] int {1, 2, 3, 4, 5}Code language: Go (go)

In this example, we initialize elements of the scores array from 1 to 5.

Alternatively, you can use the short variable declaration:

scores := [5] int { 1, 2, 3, 4, 5}Code language: Go (go)

Go can infer the length of the array based on the initial values, you can omit the size in the initialization like this:

scores := [...] int { 1, 2, 3, 4, 5 }Code language: Go (go)

Accessing elements

Go is zero-based indexing. It means that the first element of the array has an index of zero, the second element has an index of 1, and so on.

To access an element, you can use the index notation:

arrayName[index]Code language: Go (go)

For example, the following displays the first element of the scores array to the console:

package main

import "fmt"

func main() {
    scores := [...] int {1, 2, 3, 4, 5}
    fmt.Println(scores[0])
}Code language: Go (go)

Output:

1Code language: Go (go)

Modifying elements

To modify an element, you also use the index notation and assign a new value to the element:

arrayName[index] = newValueCode language: Go (go)

For example, the following changes the first element of the scores array to 0:

package main

import "fmt"

func main() {
    scores := [...]int{1, 2, 3, 4, 5}
    scores[0] = 0
    fmt.Println(scores[0])
}Code language: Go (go)

Output:

0Code language: Go (go)

Getting the length of an array

To get the length of an array, you can use the len() function:

len(arrayName)Code language: Go (go)

For example:

package main

import "fmt"

func main() {
    scores := [...] int {1, 2, 3, 4, 5}
    len := len(scores)
    fmt.Println(len)
}Code language: Go (go)

Output:

5Code language: Go (go)

In this example, we get the length of the scores array using the len() function and display it on the screen.

Iterating over array elements

To iterate over array elements, you can use a for loop. Here’s a simple for loop that iterates over the elements of the scores array and displays each element on the screen:

package main

import "fmt"

func main() {
    scores := [...] int {1, 2, 3, 4, 5}
    for i := 0; i < len(scores); i++ {
        fmt.Println(scores[i])
    }
}Code language: Go (go)

Output:

1
2
3
4
5Code language: Go (go)

In practice, you often use the for range to iterate elements of an array:

package main

import "fmt"

func main() {
    scores := [...] int {1, 2, 3, 4, 5}
    for index, value := range scores {
        fmt.Println(index, value)
    }
}Code language: Go (go)

The for range loop allows you to access index and value which is more readable than the traditional for loop.

Slicing

Go allows you to slice an array to create a slice. Slices are a more flexible data structure in Go. Slices are like arrays but have dynamic sizes. It means that the size of the slices can grow and shrink dynamically.

To create a slice from an array, you use the following syntax:

array[start:end]Code language: Go (go)

In this syntax:

  • start is the starting index (inclusive).
  • end is the ending index (exclusive).

The slice will contain the elements from the start to the end - 1.

For example, the following creates a slice that contains the elements 1, 2, and 3:

package main

import "fmt"

func main() {
    scores := [...]int{1, 2, 3, 4, 5}
    slices := scores[1:4]
    fmt.Println(slices)
}Code language: Go (go)

Output:

[2 3 4]Code language: Go (go)

Summary

  • Arrays are fixed-length sequences of elements with the same types.
  • Arrays are zero-based indexing.
  • Use array[index] to access an element of the array at the index.
  • Use for and for range loops to iterate over elements.
  • Use the len() function to get the length of an array.
  • Slices are like arrays but have a dynamic length.
  • Use slicing to create a slice from an array.
Was this tutorial helpful?