7 minutes read

Let's introduce ourselves to the most simple data structure in programming an array. In this topic, we'll discuss it in terms of Go. In particular, we will cover how to create it and work with it.

Array declaration

In the Go language, arrays are sequential collections of elements having the same data type. Arrays have a fixed number of elements. To declare an array, you need first to specify its size within [] , and then the type of its elements, like here:

var array [4]int

In the example above, we declare an array of integers with the size of 4 elements. After declaration, Go will populate it with elements having the default value for the given type. You can print our new array using the fmt.Print function to see what's in it:

fmt.Println(array) // Will print [0 0 0 0]

Zero is the default value for the int type, thus, our newly created array is filled with zeros.

Please mind that you can use constants to specify the size of an array, but you cannot use variables:

const s1 = 4
var s2 = 4

var a [s1]int // Will be ok
var b [s2]int // Will throw a compilation error

Since arrays always have a fixed number of elements, the Go compiler must know the amount of memory to allocate for an array at compile-time; therefore, constant values, which are created at compile-time, can be used as the array size. However, the value of a variable var is unknown until run time; therefore, you're not allowed to use a variable as the array size.

Declaration and elements assignment

You can declare an array and assign values to its elements at once:

var a = [4]int{10, 2, -2, 42}

If you don't specify the values of all the elements, Go will set the missing ones to the default value, like it's shown here:

var a = [4]int{10, 2}
fmt.Println(a)  // Will print [10 2 0 0]

Also, you can declare an array and assign values to specific elements using the pair index: value:

var a = [4]int{0: 10, 3: 2}
fmt.Println(a)  // Will print [10 0 0 2]

Keep in mind that if you have a multi-line assignment, you need to have a comma at the end of each line:

var a = [4]int{
    0: 10,
    3: 2,   // Even at the last one
}

Indexing

Now, let's have a look at how we can use arrays. First of all, we will need to access an element within an array. In order to do so, we must specify the name of the array and then the index of the required element, within []. In Go, the first element starts with the index zero. To print the last fourth in this case element, we can write the following:

var a = [4]int{10, 2, -2, 0}
fmt.Println(a[3]) // Will print 0

This a[3] is equivalent to a normal variable. Hence, we can assign a new value to it:

a[3] = 42
fmt.Println(a) // Will print [10 2 -2 42]

Unlike declaration, indexing can use variables. Keep in mind a Go program will panic if an index goes out of the array boundary during runtime:

var i = 3
fmt.Println(a[i]) // Will print 42

i++ // Incrementing i
fmt.Println(a[i]) // Runtime error: index out of range [4] with length 4 

Retrieving an element from an array using a variable as an index is one of the key features of an array. Later on, with the help of a loop, we will be able to perform bulk operations on arrays' elements. We don't need to think about it just yet, though: we will learn about it in one of the following topics.

Array assignment

Array itself is a type with two properties: size and type of elements. Consider the following example:

var a = [4]int{1, 2, 3, 4}
var b [3]int

b = a  // Error: cannot use a (type [4]int) as type [3]int in assignment

This will throw a compile error. For us to be able to assign a to b , they must have the same size and the same type of elements, like here:

var a = [4]int{1, 2, 3, 4}
var b [4]int

b = a          // Will be ok
fmt.Println(b) // Will print [1 2 3 4]

Multidimensional arrays

As we've mentioned earlier, array itself is a type. Therefore, we can do the same trick again and create an array of arrays:

var multiArray [3][4]int

This new multi-array will obey the same rules as a regular array. Here are some examples of how we can declare a multidimensional array and assign values to its elements:

var a = [3][4]int{
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12},
}
fmt.Println(a) // [[1 2 3 4] [5 6 7 8] [9 10 11 12]]

var b = [3][4]int{
    1: {
        2: 5,
        3: 6,
    },
} 
fmt.Println(b) // [[0 0 0 0] [0 0 5 6] [0 0 0 0]]

If we try to get a value using an index, we will get the inner array as a value:

var v = a[1] // Will assign [5 6 7 8] to the v variable 

We can again use an index to obtain the final value:

var v = a[1][2] // Will assign 7 to the v variable 

And now, just like with regular arrays, their types need to match exactly to assign one to another. Consider these examples:

var a [2][3]int
var b [3][2]int

a = b // ERROR

var c [2][3]float32
var d [2][3]int

c = d // ERROR

var e [2][3]string
var f [2][3]string

e = f // OK

Conclusion

Well done for making it here! Here's a summary of what we have learned today:

  1. What an array is

  2. How to declare and use it in Go

  3. Array as a type

  4. How to declare and use multidimensional arrays

Arrays are great. But they have a fixed size and are not used in Go often. Most of the time, we'll work with slices – a more flexible analog of arrays. We'll discuss them in the next topic.

99 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo