Updated April 12, 2023
Introduction to Go Slice
In Go language the Slice is a data type that allows us to store similar types of data types, we can say a slice is similar to any array in go language. The only difference from an array to slice is in slice we can define the length of the array, slice also contains the numeric indexes which start from 0 upto length-1, We can only place similar type of data types in the slice, in case if we try to place any non-similar data then it will throw an error for example if we defined marks slice as int then in mark slice, we can only put integer marks not string.
Syntax
Below we have written a very simple syntax,
var name_slice[Length] Data Type
In the below, there are three important things that we have explained below.
- name_slice: This is the name of the slice, this name can be anything according to our uses, for example in case if it is a list of the students’ marks then we can give it name as marks and only marks of the students will be there, in the same way, it can be a list of the name of the student then we can name it as the student and all the name of the students will be there.
- Length: This is the length of the array which we can take any number like 2,5,6 and this will be the length of the array which we have defined.
- Data Type: This is the data type of the attributes which we are going to store in the slice, for example, if we want to store the registration number of the students then we will take the type as the int and it will allow only numeric registration to store here.
How does Slice work in the Go language?
Working is explained in the following steps,
- Once we define the variable [], the compiler will expect the data type of the variable, so that it will manage its memory-specifically defined data type.
- If we pass any length for the variable like [length], which means we are notifying the compiler for the defined length for the array, so if we defined length as the 7 so it will accommodate the length upto 7.
- We can access any index of the slice with the operator: like [:1],[1:0].
Examples
In the all below example to execute we can create any file like slice.go and paste the code of the example and we can run the command go run slice.go and we can see the output of the examples.
Example #1
Below is the example where we are assigning some string value to the variable we defined as the slice. Here we have defined slice as the string so we are only allowed to store here-string values, if we try to store int value it will throw an error.
Code:
// Here we are writing a golang program to
// describe the flow and uses of the slice
package main
import "fmt"
func main() {
// Here we are constructing some array which contains details
detailArr := [7]string{"Hello", "my", "name", "is",
"Ranjan", "Kumar", "Pandey"}
// Printing the detail array
fmt.Println("The detail array values are:", detailArr)
// Extracting some specific portions of the array
detailSlice:= detailArr[1:6]
// Printing the extracted value of arrays
fmt.Println("The array after extracting specific values:", detailSlice)
// Finding the length of the slice array
fmt.Printf("Current length of the slice array is: %d", len(detailSlice))
// Calculating the actual capacity of the array
fmt.Printf("\nThe actual capacity of the array is: %d", cap(detailSlice))
}
Output:
Example #2
Below is a simple example where we are defining the variable type as the string and accessing all the string variables and printing them. We can see the accessing of the variables done like an array, the only difference here is we have defined the length of the slice.
Please see the below example.
Code:
package main
import "fmt"
func main() {
arrDescription := [4]string{"Learning", "is", "best", "things"}
var description_slice_1 = arrDescription[1:2]
description_slice_2 := arrDescription[0:]
description_slice_3 := arrDescription[:2]
description_slice_4 := arrDescription[:]
fmt.Println("The actual description array is: ", arrDescription)
fmt.Println("The slice description array 1 is: ", description_slice_1)
fmt.Println("The slice description array 2 is: ", description_slice_2)
fmt.Println("The slice description array 3 is: ", description_slice_3)
fmt.Println("The slice description array 4 is: ", description_slice_4)
}
Output:
Example #3
Below is a simple example where we are running the loop over the string, by using for loop. Here we have used the len function to get the total length of the slice.
Code:
package main
import "fmt"
func main() {
description := []string{"We", "are", "in", "the",
"real", "programming", "world"}
for i := 0; i<len(description); i++ {
fmt.Println(description[i])
}
}
Output:
Example #4
Below is an example where we are working with the numeric type of slice. Here we are defining the array on integer type and accessing their indexes and also we are updating the existing value of the slice from the new values.
Code:
package main
import "fmt"
func main() {
numericArr := [6]int{89, 90, 22, 45, 90, 32}
sliceArr := numericArr[0:5]
fmt.Println("Actual array is: ", numericArr)
fmt.Println("Array after slice: ", sliceArr)
sliceArr[0] = 100
sliceArr[1] = 1000
sliceArr[3] = 1000
fmt.Println("\nThe new array is: ", numericArr)
fmt.Println("The array after slice is: ", sliceArr)
}
Output:
Conclusion
From this tutorial we learned about the basic concept of the slice in the golang, we learned about the flow of the working and understand the syntax of the slice. We focus on some of the examples of the slice which explain some of its important uses in real-world programming.
Recommended Article
This is a guide to Go Slice. Here we discuss the introduction, syntax, and working of slice functions along with different examples and code implementation. You can also go through our other suggested articles to learn more –