Learnmonkey Learnmonkey Logo

Go Arrays

What is an Array?

An array is an ordered list of values of the same data type of a fixed length. That way, instead of declaring seperate variables for each value, you could put it together into one variable. The values in an array can be called items or elements. This tutorial will use the term element.

Fun fact: Array elements are physically stored next to each other in memory. This means that accessing a value in an array is very, very fast.

Declaring an Array

The general format of making an array is:


var array_name = [length]datatype{values} // explicitly setting the array length
var array_name = [...]datatype{values} // array's length will be inferred, also, you need the three periods!
    

You replace array_name with the name of your array variable, length with the length of your array, replace datatype with the data type of your array, and replace values with your values seperated by commas.

Remember: You cannot have more elements than the length of your array!
Remember: You cannot have elements of different data types than the array data type!
If you don't add the ... in the square brackets, Go will think that you are making a slice, not an array!

You can also you the short declare operator (:=):


array_name := [length]datatype{values} // explicitly setting the array length
array_name := [...]datatype{values} // array's length will be inferred, also, you need the three periods!
    

Accessing Array Elements

To access and set array elements, we use array indices. Array indices start at zero for the first element and go up by one for each element. For example, the element with array index 2 is the third element and the element with array element 4 is the fifth element.

To access an array element, we type:

array[index]

We replace array with the name of the array variable and index with the index of the element. For example:


websites := [6]string{"GitHub", "Wikipedia", "Learnmonkey", "Twitter", "Gmail", "Stack Overflow"}
fmt.Println(websites[2])
fmt.Println(websites[5])
fmt.Println(websites[0])
fmt.Println(websites[1])
    

The above code returns:


Learnmonkey
Stack Overflow
GitHub
Wikipedia

Setting Array Elements

To set array elements, we do the exact same thing as getting array elements, but we act as if the array element is a variable. So, we type:

array[index] = value

For example:


companies := [4]string{"Google", "Facebook", "Apple", "Twitter"}
companies[1] = "Microsoft"
fmt.Println(companies)
    

The above code returns:

[Google Microsoft Apple Twitter]

Initializing Only Specific Elements

When we are making arrays, we often want to initialize only specific elements of the array. We can do this by first setting our array to an empty array and setting the values we want like this:


testArray = [5]int{}
testArray[0] = 3
testArray[2] = 6
testArray[3] = 9
testArray[4] = 1
    

Instead of doing the above and writing lots of nasty code, we could use a map to accomplish the same thing:


testArray = [5]int{0:3, 2:6, 3:9, 4:1}
    

Getting the Array Length

To get the length of an array, we use the len function. For example:


var my_array = [3]int{6,3,9}
fmt.Println(len(my_array))
    

returns:

3

Nested Arrays

We can put arrays inside of arrays, as we can do with any other data type, but the code is messier:


array1 := [2][4]int{
    {1, 2, 3, 4},
    {5, 6, 7, 8},
}
array2 := [...][...]int{
    {1, 2, 3, 4},
    {5, 6, 7, 8},
}
    

Nested arrays can represent tables and matrices.