Arrays in Go

In Go, an array is used to store multiple values of the same data type in a single variable. It can also be described as a numbered sequence of elements of a specific length.

An array's length is part of its type, so arrays are of fixed size and cannot be resized.

Arrays can be declared in two ways:

  • Using var keyword
  • Using the shorthand declaration :=

Array Declaration using var Keyword

In Go, an array can be declared using the var keyword with an array name, length, and data type.

var arrayName[length] datatype
var arrayName = [length] datatype{ values }
var arrayName = [...] datatype{ values }

The following declares an empty array. A variable named nums as an array of five elements of int type. Since we have not assigned any values, so values will be the default value of int type which is 0.

Example: Declaring Empty Array
var nums[5] int

fmt.Println(nums) //output: 0 0 0 0 0

The following declares and initialize arrays using the var keyword.

Example: Array Declaration and Initialization
var oddnums = [5]int { 1, 3, 5, 7, 9}
var cities = [...]string {"Mumbai","New York","London","Paris",}

fmt.Println(oddnums) //output: [1 3 5 7 9]
fmt.Println(cities) //output: [Mumbai New York London Paris] 

In the above example, oddnums is declared and initialized with five elements. [5]int specifies the length of an array.

The cities array initialized with inferred lengths by using using ..., without specific length.

Declare Array Using Shorthand Declaration

In Go, you can use := to declare an array.

arrayName := [length] datatype {values}
arrayName := [...] datatype {values} 

In the following example, array2 are declared using the shorthand declarations.

Example: Shorthand Declarations of Arrays
oddnums := [5]int { 1, 3, 5, 7, 9}
cities := [...]string {"Mumbai","New York","London","Paris",}

fmt.Println(oddnums) //output: [1 3 5 7 9]
fmt.Println(cities) //output: [Mumbai New York London Paris] 

Array Initialization

In Go, an array can be initialized, partially initialized, or not initialized. If an array is not initialized, the elements of the array will take the default value of the array data type. For int, the default is 0 and for string it is "".

Example: Array Initialization
array1 := [4]int {} //uninitialized, empty array
array2 := [4] int {5, 6} //partially initialized
array3 := [4] int {5, 6, 7, 8} //fully initialized

fmt.Println(array1) //output: [0 0 0 0]
fmt.Println(array2) //output: [5 6 0 0]
fmt.Println(array3) //output: [5 6 7 8]

Specific elements in an array can be initialized as shown below:

Example: Shorthand Array Initialization
nums := [4] int {1:6, 2:7}
fmt.Println(nums) //output: [0 6 7 0]

In the above example, 1:6 means, 1st index (second element) will be 6, and 2:7 means 2nd index (third element) will be 7, and rest will be default values of int type, which is 0.

Array Length

The length (or size) of an array denotes the number of elements it can contain. The length of an array is always remain fixed and cannot be changed. If you need to increase or decrease the length then you should either create a new array or create a slice. The len() function returns the length of the specified array.

Example: len()
oddnums := [5]int { 1, 3, 5, 7, 9}
cities := [...]string {"Mumbai","New York","London","Paris",}

fmt.Println(len(oddnums)) //output: 5
fmt.Println(len(cities)) //output: 4

Accessing Array Elements

The values in the array are called elements. Each element will have an index. The first element's index is 0 and the second element's index is 1 and so on. The square bracket [] is used to specify the index of an element. For example, array[2] will return the third element from the array.

Example: Access Array Elements
oddnums := [5]int { 1, 3, 5, 7, 9}
fmt.Println(oddnums[0]) //1
fmt.Println(oddnums[1]) //3
fmt.Println(oddnums[2]) //5
fmt.Println(oddnums[3]) //7
fmt.Println(oddnums[4]) //9

cities := [...]string {"Mumbai","New York","London","Paris",}
fmt.Println(cities[0]) //Mumbai
fmt.Println(cities[1]) //New York
fmt.Println(cities[2]) //London
fmt.Println(cities[3]) //Paris
//fmt.Println(cities[4]) //error: index 4 out of bounds [0:4]

Note that trying to access elements out of index range will throw an error. It is recommended to check array length before accessing.

Update Array Elements

The values of an array can be changed by using the element's index as shown in the following example.

Example: Update Array Elements
cities := [...]string {"Mumbai","New York","London","Paris",}
cities[0] = "Delhi"
cities[2] = "Liverpool"
//cities[4] = "Liverpool"//error: index 4 out of bounds [0:4]

fmt.Println(cities[0]) //Delhi
fmt.Println(cities[2]) //Liverpool

Loop through an Array

You can loop through the elements of an array using the for loop and the len() method.

Example: Looping an Array
var nums = [4]int { 5, 10, 15, 20}

for i := 0; i < len(nums); i++ {
	fmt.Println(nums[i]) // print each element