Working with Arrays in Go(golang)

In every programming language, there are ordered series or arrangements of elements called arrays. But Go handles arrays differently compared to some other like JavaScript or Ruby. Let us take a look at Arrays, how they’re structured, and how to write them in Go! These examples were heavily influenced by Michael Van Sickle’s course posted on freecodecamp and the link for the resource will be at the bottom.
Arrays
Go is a statically typed language elements within the array must be of the same data type. Arrays in Go must be of a fixed length meaning that if you initializing an array to have 3 elements, that means that the program is allocating that much memory to the length of that array, no more and no less. When declaring an array, we start with the square brackets with the size of the array within those brackets. It is then followed by the datatype that the array can store.
var students = [3]string
If you want to initialize the array with values next to the datatypes you place curly brackets with the values.
var numbers = [3]int{27, 42, 11}
When initiating arrays with values, you don’t need to have an index of the array within the square brackets. Instead, you can fill it with […] which means set the array length equal to the number of elements you’re initializing with.
var animals = [...]string{"Lion", "Gorilla", "Cat", "Dog"}
If we wanted to check the length of an array Go has a function “len( )” which will return us back the length of the element you pass in.
students := [...]string{"Sean", "Elaina", "Clark"}
fmt.Println(len(students)) //method used to print to console//returns 3 as we have 3 elements in the array
Nested Arrays
So we know now how to make an array, let’s talk about how we can make an array of arrays also known as nested arrays.
var twoDimendionalArray[3][3]int = [3][3]int{[3]int{1, 1, 0}, [3]int{0, 1, 1}, [3]int{0, 0, 1}}
You might be thinking “Whoa, making an array looked so simple, now there's so many 3’s and 1' and 0’s. What's going on here?” let’s write this out in a different way that might be a little bit easier to read and break it down.
/*
We initializing our array to contain 3 elements within the array and the arrays within that array are going to also contain 3 elements.
arrays are 0 indexed, meaning that they start from zero instead of starting at 1.we are then assigning first element of the array to contain an array with 1, 1, 0 and following the same process with the second and third element in the array
*/var twoDimendionalArray[3][3]int
twoDimendionalArray[0] = [3]int{1, 1, 0}
twoDimendionalArray[1] = [3]int{0, 1, 1}
twoDimendionalArray[2] = [3]int{0, 0, 1}
So if we were to print this out, it would give us this result
[
[1,1,0]
[0,1,1]
[0,0,1]
]
Array Pointer
In Go arrays are considered values meaning that if we have the variable array1 store an array, and we have the variable array2 being assigned to array1. array2 is not array1 but an exact copy of array1. So since array2 is just a copy of array1 and changes that are made will not affect the other. But depending on the size of the array, this could take up a lot of memory since we are just making a whole new copy of the first array.
array1 := int[3]{33,63,74}
array2 := firstArray
Say we did want to assign a new array but have it affect the original array as well. That is where pointers come in!
array1 := int[3]{33,63,74}
array2 := &array1
array2[0] = 69fmt.Println(array1)
fmt.Println(array2)
//Output:
/*
[69,63,74]
[69,63,74]
It changes both of the arrays because array2 is not pointing to array1 instead of just making a copy of array1
*/
That is all the basics of arrays for now, in my next code along i’ll be going over ways we can manipulate arrays, through slices!
Resources
Learn Go Programming — Golang Tutorial for Beginners: