← Blog

Go Programming Language — Arrays

2020-04-27 · 2 min read

goprogramming

Go Programming Language — Arrays

Go Programming Language

Arrays are a familiar data structure — a fixed-size, ordered collection of elements of the same type. When you declare an array in Go, you specify both the element type and the size (maximum number of elements). Neither can be changed after declaration.

Arrays also serve as the foundation for slices — a Go-specific construct I’ll cover in a follow-up post.

You access an element by its index and assign values using the = operator.

The basic declaration syntax:

variableName := [size]type{}
variableName := [size]type{value1, value2, ..., valueN}

Example — declaring and populating a string array of size 3:

package main

import (
  "fmt"
)

func main() {
  // Method 1: declare then assign
  games1 := [3]string{}
  games1[0] = "Witcher"
  games1[1] = "PES"
  games1[2] = "CS:GO"
  fmt.Printf("games1: %v \n", games1)

  // Method 2: initialize with values
  games2 := [3]string{"Witcher", "PES", "CS:GO"}
  fmt.Printf("games2: %v\n", games2)
}

Output:

games1: [Witcher PES CS:GO]
games2: [Witcher PES CS:GO]

Any index that is not explicitly assigned gets the zero value for the array’s element type. For example, when games1 is first declared (before any assignments), all three slots hold "" — the zero value for string:

package main

import (
  "fmt"
)

func main() {
  games   := [3]string{}
  fmt.Printf("games: %v \n", games)

  flags   := [3]bool{}
  fmt.Printf("flags: %v \n", flags)

  numbers := [3]int{}
  fmt.Printf("numbers: %v \n", numbers)
}

Output:

games:   [  ]  // ["", "", ""]
flags:   [false false false]
numbers: [0 0 0]

Accessing an index outside the array’s bounds results in an out-of-bounds error:

package main

func main() {
  games := [3]string{}
  games[3] = "Mario"
}

Output:

./prog.go:5:9: invalid array index 3 (out of bounds for 3-element array)

A standard array declaration produces a one-dimensional array. Go also supports multi-dimensional arrays (matrices):

type Matrix [3][3]string

func main() {
  m := Matrix{
    {"a", "b", "c"},
    {"1", "2", "3"},
    {"x", "y", "z"},
  }

For iteration, Go supports all standard loop forms. Here are several approaches using an integer array:

package main

import "fmt"

func main() {
  arr := [3]int{0, 1, 2}

  // Standard for loop
  fmt.Println("For v1")
  for i := 0; i < len(arr); i++ {
    fmt.Println(arr[i])
  }

  fmt.Println("Continue & Break")
  // continue & break
  for i := 0; i < len(arr); i++ {
    fmt.Println(arr[i])
    if i == 0 {
      // Jump back to the loop start
      continue
    }
    if i == 2 {
      // Exit the loop
      break
    }
    fmt.Println("loop continued")
  }

  fmt.Println("For v2")
  // Infinite loop with manual break
  var i int
  for {
    fmt.Println(arr[i])
    i += 1
    if i >= len(arr) {
      break
    }
  }

  fmt.Println("Range")
  // Range-based iteration
  for index, value := range arr {
    fmt.Println(index, value)
  }
}