Working with Arrays in Golang

An array is a fixed-size collection of elements of the same type. The elements of the array are stored sequentially and can be accessed using their index.

Golang Array Illustration

Declaring an Array in Golang

You can declare an array of length n and type T like so -

var a[n]T

For example, here is how you can declare an array of 10 integers -

// An array of 10 integers
var a[10]int

Now let’s see a complete example -

package main
import "fmt"

func main() {
	var x [5]int // An array of 5 integers
	fmt.Println(x)

	var y [8]string // An array of 8 strings
	fmt.Println(y)

	var z [3]complex128 // An array of 3 complex numbers
	fmt.Println(z)
}
# Output
[0 0 0 0 0]
[       ]
[(0+0i) (0+0i) (0+0i)]

By default, all the array elements are initialized with the zero value of the corresponding array type.

For example, if we declare an integer array, all the elements will be initialized with 0. If we declare a string array, all the elements will be initialized with an empty string "", and so on.

Accessing array elements by their index

The elements of an array are stored sequentially and can be accessed by their index. The index starts from zero and ends at length - 1.

package main
import "fmt"

func main() {
	var x [5]int // An array of 5 integers

	x[0] = 100
	x[1] = 101
	x[3] = 103
	x[4] = 105

	fmt.Printf("x[0] = %d, x[1] = %d, x[2] = %d\n", x[0], x[1], x[2])
	fmt.Println("x = ", x)
}
# Output
x[0] = 100, x[1] = 101, x[2] = 0
x =  [100 101 0 103 105]

In the above example, Since we didn’t assign any value to x[2], it has the value 0 (The zero value for integers).

Initializing an array using an array literal

You can declare and initialize an array at the same time like this -

// Declaring and initializing an array at the same time
var a = [5]int{2, 4, 6, 8, 10}

The expression on the right-hand side of the above statement is called an array literal.

Note that we do not need to specify the type of the variable a as in var a [5]int, because the compiler can automatically infer the type from the expression on the right hand side.

You can also use Golang’s short variable declaration for declaring and initializing an array. The above array declaration can also be written as below inside any function -

// Short hand declaration
a := [5]int{2, 4, 6, 8, 10}

Here is a complete example -

package main
import "fmt"

func main() {
	// Declaring and initializing an array at the same time
	var a = [5]int{2, 4, 6, 8, 10}
	fmt.Println(a)

	// Short declaration for declaring and initializing an array
	b := [5]int{2, 4, 6, 8, 10}
	fmt.Println(b)

	// You don't need to initialize all the elements of the array.
	// The un-initialized elements will be assigned the zero value of the corresponding array type
	c := [5]int{2}
	fmt.Println(c)
}
# Output
[2 4 6 8 10]
[2 4 6 8 10]
[2 0 0 0 0]

Letting Go compiler infer the length of the array

You can also omit the size declaration from the initialization expression of the array, and let the compiler count the number of elements for you -

package main
import "fmt"

func main() {
	// Letting Go compiler infer the length of the array
	a := [...]int{3, 5, 7, 9, 11, 13, 17}
	fmt.Println(a)
}
# Output
[3 5 7 9 11 13 17]

Exploring more about Golang arrays

  1. Array’s length is part of its type

    The length of an array is part of its type. So the array a[5]int and a[10]int are completely distinct types, and you cannot assign one to the other.

    This also means that you cannot resize an array, because resizing an array would mean changing its type, and you cannot change the type of a variable in Golang.

    package main
    
    func main() {
    	var a = [5]int{3, 5, 7, 9, 11}
    	var b [10]int = a  // Error, a and b are distinct types 
    }
  1. Arrays in Golang are value types

    Arrays in Golang are value types unlike other languages like C, C++, and Java where arrays are reference types.

    This means that when you assign an array to a new variable or pass an array to a function, the entire array is copied. So if you make any changes to this copied array, the original array won’t be affected and will remain unchanged.

    Here is an example -

    package main
    import "fmt"
    
    func main() {
    	a1 := [5]string{"English", "Japanese", "Spanish", "French", "Hindi"}
    	a2 := a1 // A copy of the array `a1` is assigned to `a2`
    
    	a2[1] = "German"
    
    	fmt.Println("a1 = ", a1) // The array `a1` remains unchanged
    	fmt.Println("a2 = ", a2)
    }
    # Output
    a1 =  [English Japanese Spanish French Hindi]
    a2 =  [English German Spanish French Hindi]

Iterating over an array in Golang

You can use the for loop to iterate over an array like so -

package main
import "fmt"

func main() {
	names := [3]string{"Mark Zuckerberg", "Bill Gates", "Larry Page"}

	for i := 0; i < len(names); i++ {
		fmt.Println(names[i])
	}
}
# Output
Mark Zuckerberg
Bill Gates
Larry Page

The len() function in the above example is used to find the length of the array.

Let’s see another example. In the example below, we find the sum of all the elements of the array by iterating over the array, and adding the elements one by one to the variable sum -

package main
import "fmt"

func main() {
	a := [4]float64{3.5, 7.2, 4.8, 9.5}
	sum := float64(0)

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

	fmt.Printf("Sum of all the elements in array  %v = %f\n", a, sum)
}
# Output
Sum of all the elements in array  [3.5 7.2 4.8 9.5] = 25.000000

Iterating over an array using range

Golang provides a more powerful form of for loop using the range operator. Here is how you can use the range operator with for loop to iterate over an array -

package main

import "fmt"

func main() {
	daysOfWeek := [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}

	for index, value := range daysOfWeek {
		fmt.Printf("Day %d of week = %s\n", index, value)
	}
}
# Output
Day 0 of week = Mon
Day 1 of week = Tue
Day 2 of week = Wed
Day 3 of week = Thu
Day 4 of week = Fri
Day 5 of week = Sat
Day 6 of week = Sun

Let’s now write the same sum example that we wrote with normal for loop using range form of the for loop -

package main
import "fmt"

func main() {
	a := [4]float64{3.5, 7.2, 4.8, 9.5}
	sum := float64(0)

	for index, value := range a {
		sum = sum + value
	}

	fmt.Printf("Sum of all the elements in array %v = %f", a, sum)
}

When you run the above program, it’ll generate an error like this -

# Output
./array_iteration_range.go:9:13: index declared and not used

Go compiler doesn’t allow creating variables that are never used. You can fix this by using an _ (underscore) in place of index -

package main
import "fmt"

func main() {
	a := [4]float64{3.5, 7.2, 4.8, 9.5}
	sum := float64(0)

	for _, value := range a {
		sum = sum + value
	}

	fmt.Printf("Sum of all the elements in array %v = %f", a, sum)
}

The underscore (_) is used to tell the compiler that we don’t need this variable. The above program now runs successfully and outputs the sum of the array -

# Output
Sum of all the elements in array [3.5 7.2 4.8 9.5] = 25.000000

Multidimensional arrays in Golang

All the arrays that we created so far in this post are one dimensional. You can also create multi-dimensional arrays in Golang.

The following example demonstrates how to create multidimensional arrays -

package main

import "fmt"

func main() {
	a := [2][2]int{
		{3, 5},
		{7, 9},	// This trailing comma is mandatory
	}
	fmt.Println(a)

	// Just like 1D arrays, you don't need to initialize all the elements in a multi-dimensional array.
	// Un-initialized array elements will be assigned the zero value of the array type.
	b := [3][4]float64{
		{1, 3},
		{4.5, -3, 7.4, 2},
		{6, 2, 11},
	}
	fmt.Println(b)
}
# Output
[[3 5] [7 9]]
[[1 3 0 0] [4.5 -3 7.4 2] [6 2 11 0]]

Conclusion

Arrays are useful but a bit inflexible due to the limitation caused by their fixed size. And this is why Go provides another data structure called Slice that builds on top of arrays and provides all the flexibility that we need. In the next article, we’ll learn about Slices.

Next Article: Introduction to Slices in Golang

Code Samples: github.com/callicoder/golang-tutorials