Before understanding 'slices', let us understand, how to access elements in an Array.
We have the 'Array' with three values, 5, 'John' and 'Go'. And we want to access the second element i.e. 'John'.
package main import "fmt" func main() { var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"} fmt.Println(arr[1]) }
So, in the above code we have created a String 'Array' and initialised the names to it.
Now, let us see, how the values are positioned in the Array
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]).
And the print statement prints the value of the second element of the 'Array' (i.e. 'John').
Now, let us understand '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'.
package main import "fmt" func main() { var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"} fmt.Println(arr[1:4]) }
So, in the above code we have created a 'Array' and initialised to the variable 'arr'.
Now, let us see, how the values are positioned in the Array
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.
And the print statement prints the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').
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'.
package main import "fmt" func main() { var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"} fmt.Println(arr[:3]) }
So, in the above code we have created a 'Array' and initialised to the variable 'x'.
Now, let us see, how the values are positioned in the Array
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.
And the print statement prints the first three elements (i.e. 'Mohan', 'John' and 'Paul').
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'.
package main import "fmt" func main() { var arr = [5]string{"Mohan", "John", "Paul", "Kriti", "Salim"} fmt.Println(arr[2:]) }
So, in the above code we have created a 'Array' and initialised to the variable 'x'.
In the below way the values are positioned in the Array,
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.
And the print statement prints the last three elements (i.e. 'Paul', 'Kriti' and 'Salim').