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




GO - SLICES


Before understanding 'slices', let us understand, how to access elements in an Array.


Accessing the elements of the 'Array' using positive Indexes


We have the 'Array' with three values, 5, 'John' and 'Go'. And we want to access the second element i.e. 'John'.


Example :



package main
import "fmt"
    
func main() {
    
    var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"}
            
    fmt.Println(arr[1])			 	
}


Output :



 John

So, in the above code we have created a String 'Array' and initialised the names to it.


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

Now, let us see, how the values are positioned in the Array


java_Collections

So, as we can see the elements are positioned as '0', '1' and '2'. And if we want to access the second element, we can refer to the position '1' using the square brackets (i.e. arr[1]).


java_Collections

And the print statement prints the value of the second element of the 'Array' (i.e. 'John').


fmt.Println(arr[1])

Now, let us understand 'Slice.'


Accessing a chunk of elements of the 'Array' using range of Indexes(slice)


We have the 'Array' with five values, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'. And we want to access the second, third and fourth element i.e. 'John', 'Paul' and 'Kriti'.


Example :



package main
import "fmt"
    
func main() {
    
    var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"}
            
    fmt.Println(arr[1:4])			 	
}


Output :



 [John Paul Kriti]

So, in the above code we have created a 'Array' and initialised to the variable 'arr'.


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

Now, let us see, how the values are positioned in the Array


java_Collections

So, as we can see the elements are positioned as '0', '1', '2', '3' and '4'.


Now, if we want to access the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').We can specify the range using ':'.


i.e. arr[1:4]


'arr[1:4]' actually tells, to pick the elements from index/position '1' to the index/position '4-1' i.e. 3.


java_Collections

And the print statement prints the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').


fmt.Println(arr[1:4])

Output :



 [John Paul Kriti]

Accessing first few elements of the 'Array' using range of Indexes(slice)


Say, we have the 'Array' with five values, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'.


And we want to access the first three elements i.e. 'Mohan', 'John' and 'Paul'.


Example :



package main
import "fmt"
    
func main() {
    
    var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"}
            
    fmt.Println(arr[:3])			 	
} 


Output :



 [Mohan John Paul]

So, in the above code we have created a 'Array' and initialised to the variable 'x'.


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

Now, let us see, how the values are positioned in the Array


java_Collections

So, as we can see the elements are positioned as '0', '1', '2', '3' and '4'.


Now, if we want to access the first three elements (i.e. 'Mohan', 'John' and 'Paul').We can specify the range using ':'.


Just that we won't be mentioning the starting point. And only put the endpoint.


i.e. arr[:3]


'arr[:3]' actually tells, to pick the elements from the start till the index/position '3-1' i.e. 2.


java_Collections

And the print statement prints the first three elements (i.e. 'Mohan', 'John' and 'Paul').


fmt.Println(arr[:3])

Output :



 [Mohan John Paul]

Accessing last few elements of the 'Array' using range of Indexes


Just like the previous example, we have the 'Array' with five values, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'.


And we want to access the last three elements i.e. 'Paul', 'Kriti' and 'Salim'.


Example :



package main
import "fmt"
    
func main() {
    
    var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"}
            
    fmt.Println(arr[2:])			 	
}


Output :



 [Paul Kriti Salim]

So, in the above code we have created a 'Array' and initialised to the variable 'x'.


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

In the below way the values are positioned in the Array,


java_Collections

So, as we can see the elements are positioned as '0', '1', '2', '3' and '4'.


Now, if we want to access the last three elements (i.e. 'Paul', 'Kriti' and 'Salim'). We can specify the range using ':'.


Just that we won't be mentioning the ending point this time. And only put the starting point.


i.e. arr[2:]


'arr[2:]' actually tells, to pick the elements from the index/position '2' till the last element.


java_Collections

And the print statement prints the last three elements (i.e. 'Paul', 'Kriti' and 'Salim').


fmt.Println(arr[2:])

Output :



 [Paul Kriti Salim]