A new product!

Social Media Toolkit

Download our all in one automation tool for various social media websites.

Gopher

Arrays in Go

Published on 15 February 2020
Last Updated on 15 February 2020

Arrays in Golang are of a specific length used for storing elements in a numbered sequence. Number of an element in an array is referred to as array element index. Index of elements stored in an array begin with 0.

How many elements can be stored in an array is determined by the length of array.

Arrays can be classified into two categories in Golang:

  • one dimensional
  • multi dimensional

Type of an array in Golang

In Golang, when an array is defined its type can be defined as [N]T, where N is the number of elements and T is the type of the array that is being defined. Similar principles apply for multidimensional arrays as well.

This means that [2]int and [3]int are separate data types even if they both are arrays of integers.

If data type of two variables is different then they can’t be compared. It also means that arrays of different lengths can’t be compared.

Example to demonstrate one dimensional array in Golang

Program given below demonstrates how one dimensional arrays can be used in Golang.

package main

import "fmt"

func main() {
	var arrayOfIntegers [8]int
	fmt.Println("Array of integers is:", arrayOfIntegers)

	// initializing array of integers with custom values
	arrayOfIntegers = [8]int{1, 2, 3, 4, 5, 6, 7, 8}
	fmt.Println("Array of integers is:", arrayOfIntegers)

	// setting an element at a custom element index
	arrayOfIntegers[3] = 0
	fmt.Println("Array of integers is:", arrayOfIntegers)

	// getting a specific array element using its index
	arrayOfIntegers[3] = 0
	fmt.Println("Array element at index 3 is:", arrayOfIntegers[3])

	/// calculating length of array
	fmt.Println("Length of array is:", len(arrayOfIntegers))
}

Program output

Above program produces following output:

Array of integers is: [0 0 0 0 0 0 0 0]
Array of integers is: [1 2 3 4 5 6 7 8]
Array of integers is: [1 2 3 0 5 6 7 8]
Array element at index 3 is: 0
Length of array is: 8

Program Description

Above program demonstrates how multidimensional arrays can be defined intilized or accessed using sample code. It also demonstrates how to set an element at a specific index or to access a specific element in an array by using its index values.

Also, using the len function in Golang, you can find length of an array that is already defined.

Example to demonstrate use of multidimensional arrays in Golang

Array of array elements is called multidimensional array in Golang. Program given below demonstrates how multidimensional arrays can be defined and used in Golang.

// program to demonstrate use of multi dimensional arrays in Golang
package main

import (
	"fmt"
	"strconv"
)

func main() {
	// defining a new multi dimensional array
	var matrix [2][3]int

	// assigning new value to existing multi dimensional array
	matrix = [2][3]int{
		[3]int{1, 2, 1},
		[3]int{3, 4, 1},
	}

	for index1 := 0; index1 < 2; index1++ {
		for index2 := 0; index2 < 2; index2++ {
			fmt.Println("element at index1 ["+strconv.Itoa(index1)+"]["+strconv.Itoa(index2)+"] is:", matrix[index1][index2]);
		}
	}

	fmt.Println("number of rows in matrix is:", len(matrix))
	fmt.Println("number of columns in matrix is:", len(matrix[0]))
}

Program output

Above program produces following output:

element at index1 [0][0] is: 1
element at index1 [0][1] is: 2
element at index1 [1][0] is: 3
element at index1 [1][1] is: 4
number of rows in matrix is: 2
number of columns in matrix is: 3

Program Description

Above program defines an array of array elements named matrix. matrix array has 2 array elements. Each array element stored in matrix array stores 3 arrays. Thus we can say that matrix array has 2 rows and 3 columns.

Above program also demonstrates how we can iterate though a multi dimensional array using a simple for loop.

Using the range operator in Golang for iterating an array

Given below is an example to demonstrate how to iterate an array element using simple for loop and range operator.

package main

import "fmt"

func main() {
	var alphabets = [3]string{
		"a", "b", "c",
	}
	for key, value := range alphabets {
		fmt.Println("key:",key)
		fmt.Println("value:",value)
		fmt.Println("");
	}
}

Program output

Above program produces following output:

key: 0
value: a

key: 1
value: b

key: 2
value: c

Program Description

As shown in above program, Golang provides a range operator that can be used for iterating through an array element.

range operator returns the index and value of array elements. range operator can also be used on multidimensional arrays in a similar way.

Use of built in len function

len is a built in function in Golang that can help you to calculate length of a an array.

In above programs, len is used as follows:

/// calculating length of array
fmt.Println("Length of array is:", len(arrayOfIntegers))

Automatic length declaration operator ...

Program given below demonstrates how ... operator can be used for automatic length declaration.

If you are un-aware about number of elements that will be stored in an array, then make use of ... operator.

// program to demonstrate use of ... operator
package main

import "fmt"

func main() {
	// ... operator used for automatic length declaration
	var alphabets = [...]string{
		"a", "b", "c",
	}
	for key, value := range alphabets {
		fmt.Println("key:",key)
		fmt.Println("value:",value)
		fmt.Println("");
	}
}

Array comparison

For comparing two arrays, both must have same number of elements and same data type.

For example [4]string can not be compared with [8]string because they are not of the same size.

For two arrays in Golang to be equal they both must have same type, same length and same number of elements for a comparison.

// program to demonstrate array comparison in Golang
package main

import "fmt"

func main() {
	arr1 := [4]int{0, 1, 2, 3}
	arr2 := [4]int{1, 2, 3, 4}
	arr3 := [4]int{1, 0, 1, 0}
	arr4 := [4]int{4, 3, 2, 1}

	fmt.Println("arr1 == arr2", arr1 == arr2);
	fmt.Println("arr2 == arr3", arr2 == arr3);
	fmt.Println("arr3 == arr4", arr3 == arr4);
	fmt.Println("arr4 == arr1", arr4 == arr1);
}

Program output

Above program produces following output:

arr1 == arr2 false
arr2 == arr3 false
arr3 == arr4 false
arr4 == arr1 false

Program to demonstrate array assignment in Golang

Program given below demonstrates various ways by which we can assign an array in Golang.

// program to demonstrate various ways to assign arrays in Golang
package main

import "fmt"

func main() {
	// method 1: initial value assignment
	arr1 := [4]int{0, 1, 2, 3}
	fmt.Println("arr1:")
	fmt.Println(arr1)

	// method 2: initial value assignment using var
	var arr2 = [4]int{0, 1, 2, 3}
	fmt.Println("\narr2:")
	fmt.Println(arr2)

	// method 3: assigning elements in array using their index
	var arr3 [4]int
	arr3[0] = 0
	arr3[1] = 1
	arr3[2] = 2
	arr3[3] = 3
	fmt.Println("\narr3:")
	fmt.Println(arr3)

	// method 4: multiline initial value assignment
	var arr4 = [4]int{
		0,
		1,
		2,
		3,
	}
	fmt.Println("\narr4:")
	fmt.Println(arr4)


	// method 5: not defining all elements in initial value assignment
	var arr5 = [4]int{0, 1, 2}
	fmt.Println("\narr5:")
	fmt.Println(arr5)

	// method 6: initial value assignment using automatic length declaration
	var arr6 = [...]int{0, 1, 2, 3}
	fmt.Println("\narr6:")
	fmt.Println(arr6)
}

Above program works in similar way for arrays of other data types and multidimensional arrays.

Program output

Above program produces following output:

arr1:
[0 1 2 3]

arr2:
[0 1 2 3]

arr3:
[0 1 2 3]

arr4:
[0 1 2 3]

arr5:
[0 1 2 0]

arr6:
[0 1 2 3]

Arrays are passed by value

When an array is passed to a function, it is passed as a value and not as a to original array. When a variable in Golang is passed as value, if any changes are made to it then they are not reflected in the original array.