
In this Blog we will be Exploring about Slices in Golang. Slices are kind of an addition to arrays. Here we can say a slice is a segment of an array. Like Arrays Slices are indexable and have a length. But Unlike arrays which has fixed length and We had to predetermine the size before implementing the arrays. This problem was solved with the Slices which has dynamic length i.e it can change and we don’t need to predetermine length.
Here is an Example :
var x []float64
The only difference between this and an array is the missing length between the
brackets. In this case, x has been created with a length of zero.
Syntax:
[]T
or
[]T{}
or
[]T{value1, value2, value3, ...value n}
Here, T is the type of the elements. For example:
var my_slice[]int
Basically there are three components of slice:
- Pointer
- Length
- Capacity
Pointer – The pointer is used to points to the first element of the array that is accessible through the slice.
Length – The length is the total number of elements present in the array.
Capacity – The capacity represents the maximum size upto which it can expand.
Let’s understand more with the help of this example –
// Golang program to illustrate the working of components
package main
import "fmt"
func main() {
// Creating an array
arr := [7]string{"Here", "is", "the", "blog",
"for", "Go", "language"}
// Display array
fmt.Println("Array:", arr)
// Creating a slice
myslice := arr[1:6]
// Display slice
fmt.Println("Slice:", myslice)
// Display length of the slice
fmt.Printf("Length of the slice: %d", len(myslice))
// Display the capacity of the slice
fmt.Printf("\nCapacity of the slice: %d", cap(myslice))
}
Let’s us take a look at the output of this program :
Array: [Here is the blog for Go language]
Slice: [is the blog for Go]
Length of the slice: 5
Capacity of the slice: 6
Now you might be wondering what are the ways for creating and initialising slice.
By Using a Slice Literal in Golang
We can create a slice using slice literal, This is similar to array but here in the case of slice lteral the size is not fixed.
this creates the same array as above, then builds a slice that references it:
[]bool{true, true, false}
var my_slice = []string{"this", "is", "blog"}
Note: Remember whenever you create a slice using a string literal, then it first creates an array and after that return a slice reference to it.
Another way to create slices is to use the [low : high]
expression:
arr := [5]float64{1,2,3,4,5}
x := arr[0:5]
low
is the index of where to start the slice and high
is the index where to end it (but not including the index itself). For example while arr[0:5]
returns [1,2,3,4,5]
, arr[1:4]
returns [2,3,4]
.
For convenience we are also allowed to omit low
, high
or even both low
and high
. arr[0:]
is the same as arr[0:len(arr)]
, arr[:5]
is the same as arr[0:5]
and arr[:]
is the same as arr[0:len(arr)]
.
By Using make() function in Golang
If you want to create a slice you should use the built-in make
function which is provided by the Go Library itself.
x := make([]float64, 5)
This creates a slice that is associated with an underlying float64
array of length 5.
The make
function also allows a 3rd parameter:
x := make([]float64, 5, 10)
var my_slice_2 = make([]int, 5, 6)
This function has taken three parameters, i.e, type, length, and capacity.
Conclusion
In This Blog we looked into basic Idea about Slices in Golang, We also discussed about the ways through which we can declare Slices in Go Language. If you liked this Blog please like and Share this Blog.
References :
Reference : If you want to learn more about slices please visit the Link1
1 thought on “A Basic Introduction about Slice in Golang.3 min read”
Comments are closed.