Hi, Today in this chapter we will learn about Arrays in Go/Golang. In Go we use Arrays to store multiple data in a singe variables. You can store all types of data in Arrays.
 
Arrays is one of powerful way to store multiple data without declaring lots of variables. We use arrays a lot in programming. Although in Go we commonly use slices but in special scenario we also use arrays.
 
There are two way to create an array. First is by syntax code and second is with short hand operator “:=”.
 
Let see how to declare array with syntax code.




 
Output:




 
As we can see in example, the syntax to declare the array is var arr = [5]int{1,6,8,9,10}.


In this example, we declared a variable with 'Var' and named it arr. And in [], we entered the length of elements it can store which is 5 after that we declared it’s data type “int” and the data in {}.

 
The amount of data or elements you can store in an array is limited. If you allotted 5 length to store data it will store only 5 elements.
 
Now see other way to declare an array.



 
As you see everything is same except the ‘:=’. This time we didn’t used Var instead we used ‘:=’.
 
I hope you know now how to make an array. Now then let’s see some other things we can do we an array.




 
In this example I made an empty array with located 5 slots with can store only 5 elements. First I stored one element 90 at position 0. When I printed array it showed that my data is stored but the rest 2 empty places array filled with value 0.
 
When we make an empty array we don’t use assigned operator like you see in example.
 
After I stored all elements in array it showed in output that array replaced 0 with the data I told it to store.
 
And I also to check the length of array by using len() which told me how many elements I can store in it.
 
There are many cases when we don’t know that how much elements or data you may need to store. In this case we put ‘…’ in place of length as you can see in example:



This way we can store many elements with limiting the array. And it won’t take unnecessary memory and fill with 0 as we saw in previous example.
 
So that’s it for this chapter. In next chapter we will learn about slices.