So far we have seen variables, which is used to store a single value. But what if we have to store 100 values?
We cannot just create 100 variables.
And to fill this gap arrays came into picture.
An 'Array' is a Data Structure that holds multiple values, of different Data Types. And you need to specify the size of the array while declaring it.
The declaration of a 'Array' is super easy.
Below is the way of declaring an int array of size 5:
Declaring an Integer Array is quite similar to declaring an integer variable.
The only additional thing you need to do is, specify the size of the Array inside the square brackets '[]' followed by the data type i.e. 'int'.
So, in the next example, we will be asking Go to store the numbers 1 to 5 in an array.
package main import "fmt" func main() { var arr [5]int for i := 0; i < 5 ; i++ { arr[i] = i + 1 } for i := 0; i < 5 ; i++ { fmt.Println(arr[i]) } }
So, in the above code, we have declared an Array 'arr' that is going to store the values from 1 to 5.
As we have seen above, the first line declares an array, 'arr' that would store 5 numbers.
Now, internally what happens is, 5 empty locations are created with indexes from '0' to '4'.
Then we have used the for loop to insert the numbers from 1 to 5 in each Iteration
for i := 0; i < 5 ; i++ { arr[i] = i + 1 }
So, in the first Iteration of for loop, the value of 'i' is '0'.
And we use this 'i' as index to access the first location of the array.
Accessing array elements is quite simple. Just put the index (i.e. 'i') inside square brackets, '[]' and you can access that element.
And what happens is, the value from the right hand side (i.e. 'i+1' or '0+1' or '1') gets assigned to the first array location (i.e. arr[0]).
So, internally the number '1' goes and sits inside 'arr[0]'.
Similarly, in the second Iteration of for loop, the value of 'i' becomes '1'.
And in the same way, we use this 'i' as index to access the second location of the array.
And as we have seen, the value from the right hand side (i.e. 'i+1' or '1+1' or '2') gets assigned to the second array location (i.e. arr[1]).
So, internally the number '2' goes and sits inside 'arr[1]'.
Continuing in the same way, all the numbers are assigned to all the 5 locations in the Array.
Now that we have all the 5 numbers stored in the Array. We can use another 'for loop' to display all the numbers.
for i := 0; i < 5 ; i++ { fmt.Println(arr[i]) }
The above 'for loop' picks all the numbers one by one and displays on the screen.
Declaring an Array that is going to hold 5 String/names is quite similar as creating an Array that holds numbers.
Just replace 'int' with 'string' and we are done.
So, in the next example, we will be asking Go to store five names, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim' in the Array.
package main import "fmt" func main() { var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"} for i := 0; i < 5 ; i++ { fmt.Println(arr[i]) } }
So, in the above code, we have declared an Array 'arr' that is going to store five names,'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'
And that's something we have done in the very first line.
Well! It's a little different than creating an empty Array.
Now, internally what happens is, 5 locations are created to hold values of String type with indexes from '0' to '4'.
And the names are assigned to the respective location, one by one.
Then we have used the for loop to to display the names in each Iteration
for i := 0; i < 5 ; i++ { fmt.Println(arr[i]) }
And we get the below output.
Next, let us see, how to access the elements of the Array in the next tutorial.