Array in Go is unlike the typical array you see in other programming languages especially those that are dynamic languages. The main reason why an array exists in Go is to be a backing store for a slice.
Because Go is statically typed, you don't have the freedom to read or write from an array as you wish without getting spanked with an error, like when you mistakenly want to write or read past an array.
Before we move any further, let's have a look at the various ways to declare an array in GO:
1. var arr1 [4]int // array declaration
2. var arr2 [4]int = [4]int{1,2,3,4} // array literal
3. var arr3 = [4]int{1,2,3,4} // array literal with automatic type assignment
4. var arr4 = [...]int{1,2,3,4} // array literal, automatic type and length assigment
5. arr5 := [4]int{1,2,3,4} // concise array literal
6. arr6 := [4]int{1:2,3:4} // prefilled values
One can simply put it, an array is a fixed-length collection of a contiguous set of elements that has the same type. If you take a look at each declaration above. you will notice there's a number in a square bracket (length of the array) and the type of elements the array will contain.
1. var arr1 [4]int
2. arr1[0] = 43
3. arr1[1] = 22
4. arr1[2] = 10
5. arr1[3] = 20
6. // Any assignment beyond this will result to: index 4 out of bounds
7. arr1[4] = 32
The declaration above will create an array and assign it to the variable named arr1. Every Go declaration like this will automatically be initialized with a zero value for the type specified. In this case, 0 is for the int type. false being the zero value for a boolean and empty string ('') for string type.
1. var arr2 [4]int = [4]int{1,2,3,4} // array literal
The arr2 in the example above both declare and initialized values the array will contain on a single line. This is called array literal
1. var arr3 = [4]int{1,2,3,4} // array literal with automatic type
You can omit the type of array to make your code clear and concise. Go will do that for you through the assignment value [4]int{1,2,3,4}.
1. var arr4 = [...]int{1,2,3,4} // array literal, automatic type and length assigment
The declaration above is different from the previous one by the ...
operator. What this does is tell the GO compiler to determine the length of the array by itself during compilation time.
1. arr5 := [4]int{1,2,3,4} // concise array literal
if you are coming from a language like Python, you would prefer to declare your variables this way. Go says, hey, I want you to experience easy life with all the glad tidings I come with. You can declare your variable this way and I will make it my job to add the needed parameters to it in the background. But if you want to do this, It should be done inside functions only.
// Specify which of the array values you currently want to store
// and at a particular index position you want to store it at
var testArr = [4]int{1:2, 3:4} // output: [0, 2, 0, 4]
The above declaration might come in handy when you only want to prefill an array at the position you want.
Go also allows you to have a pointer as array values. Let's have a brief look
1. var pointerArr = [3]*int{new(int), new(int), new(int)}
// output: [0xc00001e088 0xc00001e0a0 0xc00001e0a8]
Array assignment
Assigning a value to an array is more like saying x = 2 in mathematics. The only difference is adding [index]
in front of the x like so x[1] = 2
You might say what does the index mean?
Let's take for instance you have multiple rooms in a building. Inside each room, there exist some things you keep inside. so how do you access or get what you store inside? that's right, you do that by entering the building and opening the door to which what you want is.
Looking up an element inside an array in GO is more like this, accessing an array element is like telling GO, hey, this so and so array variable name (building) you have declared, go inside it, then enter the room (index) so and so and get me the value you see there.
Let's get into detail:
1. var arr1 = [4]int{1,2,3,4}
2. // you access an element through what we call indexing
3. arr1[0] = 5 // the arr1[0] will change the first element of the array to 5
4. arr[1] = 6 // the arr1[1] will change the second element of the array to 6
5. arr[2] = 7 // the arr1[2] will change the third element of the array to 7
6. arr[3] = 8 // the arr1[3] will change the fourth element of the array to 8
As you might have inferred from the code above, index 0 corresponds to the first value of the array, index 1 corresponds to the second value, index 2 to the third value and so on. This is exactly what is called array indexing.
1. var pointerArr = [3]*int{new(int), new(int), new(int)}
// output: [0xc00001e088 0xc00001e0a0 0xc00001e0a8]
// The unreadable value the pointerArr is holding here is basically the address that points to the value in memory
Accessing and changing the value the memory address is pointing to is easy. You do this like so:
1. var pointerArr = [3]*int{new(int), new(int), new(int)}
2. *pointerArr[0] = 45
3. *pointerArr[1] = 10
4. *pointerArr[2] = 20
The * is called de-referencing, it's instructing GO that, I know you are only giving me the value I stored inside the array, in this case, an address. Now I am giving you the full authority to look for that address inside my hard disk and get me the value you find there.
The next operation is the assignment operator telling Go that it should overwrite the current value the address holds to the new value at the right of the = operator
Assigning one array to another array
There's one important concept you must understand before you perform this operation if you don't want the compiler to slap your face with an error. You need to understand that you can only assign an array of the same type to each other and it is not that simple.
1. var arr1 [7]int = [7]int{1,2,3,4,5,6,7}
2. var arr2 [4]int = [4]int{8,9,10,11}
3. arr2 = arr1
The above will run with an error in your face. Why? Because Go does not recognize those two declared arrays as being the same type.
Array type constitutes array size and the type of value(s) it holds. So, looking at the above code again now makes perfect sense. The arrays are definitely of type int but their size is different. The solution to the error prompted by the compiler is like so:
1. var arr1 [7]int = [7]int{1,2,3,4,5,6,7}
2. var arr2 [7]int = [7]int{8,9,10,11,12,13,14}
3. arr2 = arr1 // output: arr2 = [1,2,3,4,5,6,7]
If you are familiar with JavaScript, you would say the arr1
is copied by reference, therefore arr2
will share the same memory address with arr1
. Go says, we don't do that here! What it does is, it copies all the values in the arr1
to arr2
. So changes made in one do not reflect in the other.
Thanks for reading up to this point and be on the lookout for more! Gracias