Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - ARRAYS


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.


Declaring an empty Array to hold numbers


Below is the way of declaring an int array of size 5:


var arr [5]int

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'.


java_Collections

So, in the next example, we will be asking Go to store the numbers 1 to 5 in an array.


Example :



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])
    }			 	
}


Output :



 1
 2
 3
 4
 5

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.


var arr [5]int

Now, internally what happens is, 5 empty locations are created with indexes from '0' to '4'.


java_Collections

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
}

1st Iteration


So, in the first Iteration of for loop, the value of 'i' is '0'.


java_Collections

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.


arr[i] = i + 1

java_Collections

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]'.


java_Collections

2nd Iteration


Similarly, in the second Iteration of for loop, the value of 'i' becomes '1'.


java_Collections

And in the same way, we use this 'i' as index to access the second location of the array.


arr[i] = i + 1

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]'.


java_Collections

Continuing in the same way, all the numbers are assigned to all the 5 locations in the Array.


java_Collections

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.


Output :



 1
 2
 3
 4
 5

Declaring an Array to hold Strings


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.


var arr [5]string

So, in the next example, we will be asking Go to store five names, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim' in the Array.


Example :



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])
    }			 	
}


Output :



 Mohan
 John
 Paul
 Kriti
 Salim

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.


var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"}

Well! It's a little different than creating an empty Array.


java_Collections

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.


java_Collections

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.


Output :



 Mohan
 John
 Paul
 Kriti
 Salim

Next, let us see, how to access the elements of the Array in the next tutorial.