Arrays in Golang

  • 12 April 2020
  • ADM

 

Arrays in Golang - images/logos/golang.jpg

 

An array is a data structure with a fixed size containing elements (items) of the same type.

Declaring an Array in Golang

You can declare an array a of length n with type T using the following syntax.

var a[n]T

Using concrete examples:

//array of 6 integers
var a0 [6]int
//array of 10 strings
var a1 [10]string

Initializing an array with an array literal

An array can be declared and initialized at the same time. An array literal has the number of elements that it will hold in square brackets, followed by the type of its elements.

//array declaration and initialization
var a2 = [6]int{10, 5, 44, 7, 22, 99}

Note: the type was not necessary to be specified in the left side, the compiler will automatically infer the type by evaluating the expression on the right side.

You can also use the Golang's short variable declaration to declare and initialize an array.

//array short declaration and initialization
a2 := [6]int{10, 5, 44, 7, 22, 99}

Infer the length of the array

You can use [...] instead of specifying the length. In this case, the compiler will identify the length of the array based on the numbers of elements specified in the declaration.

//array declaration and initialization without specifying the length
a3 := [...]int{10, 5, 44, 7, 22, 99}

Array's Properties

The length of an array is part of its type: The arrays can not be resized. Also, you can not assign the array to a new variable with a different length.

//array of 10 strings
var a1 [10]string
//array declaration and initialization
var a2 = [6]int{10, 5, 44, 7, 22, 99}

a1 = a2

The error thrown at the compilation will be:

# command-line-arguments
.\example.go:12:5: cannot use a2 (type [6]int) as type [10]string in assignment

Arrays in Golang are value types: when you assign an array to a new variable or pass an array to a function, the entire array is copied. This would mean that any changes in the copied array will not affect the original array.

Iterating over an array in Golang

Using for loop

You can use the for loop to iterate over an array.

Example

package main

import "fmt"

func main() {

	//array declaration and initialization
	var a2 = [6]int{10, 5, 44, 7, 22, 99}

	sum := 0

	for i := 0; i < len(a2); i++ {
		sum = sum + a2[i]
	}

	fmt.Println("Array iteration using for loop")
	fmt.Println("Array: ", a2)
	fmt.Println("Sum = ", sum)
}

The len() function is used in the for loop to find the length of the array.

Output

Array iteration using for loop
Array:  [10 5 44 7 22 99]
Sum =  187

Using for range loop

Example

package main

import "fmt"

func main() {

	//array declaration and initialization
	var a2 = [6]int{6, 5, 23, 7, 13, 67}

	sum := 0

	for _, val := range a2 {
		sum = sum + val
	}

	fmt.Println("Array iteration using for range loop")
	fmt.Println("Array: ", a2)
	fmt.Println("Sum = ", sum)
}

If you need the index inside of the for range loop replace the code with the following:

for index, val := range a2 {
	//index can be used now
	sum = sum + val
}

Output

Array iteration using for range loop
Array:  [6 5 23 7 13 67]
Sum =  121

 

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build example.go

Assuming that example.go is the name of your file.

Then depending on if you are on Linux or Windows the binary file is created.

To run the application, execute the command.

Linux

$ ./example

Windows

c:\Users\adm\go\tutorials> example.exe

If you want to compile and run the application in one single step, run the following command:

go run example.go

 

References