In the Go language, a Slice is a key data type that is powerful and flexible as compared to an array. Slice is an abstraction over an array and overcomes limitations of arrays like getting a size dynamically or creating a sub-array of its own and hence much more convenient to use than traditional arrays. It is just like an array, having an index value and length, but the size of the slice is resized they are not in fixed-size just like an array. Slices only store elements of the same type, different types of elements cannot be stored in the same slice.
A slice is formed by specifying two indices, a low and high bound, separated by a colon as illustrated below:
arr[low_bound : high_bound]
This includes the low_bound, but excludes the high_bound, where the smallest value of low_bound can be 0 and largest value of high_bound can be the length of arr array.
To make a slice of slices, we can compose them into multi-dimensional data structures similar to that of 2D arrays. This is done by using a function called make() which creates an empty slice with a non-zero length. This is shown in the following example:
Go
package main
import (
"fmt"
)
func main() {
slice_of_slices := make([][]int , 3)
for i := 0; i < 3; i++ {
slice_of_slices[i] = make([]int, 3)
for j := 0; j < 3; j++{
slice_of_slices[i][j] = i * j
}
}
for i := 0; i < 3; i++ {
fmt.Println(slice_of_slices[i])
}
fmt.Println("Slice of slices: ", slice_of_slices)
}
|
Output:
[0 0 0]
[0 1 2]
[0 2 4]
Slice of slices: [[0 0 0] [0 1 2] [0 2 4]]
It is possible to have a variable slice of slices due to their dynamic nature. This is shown in the example below:
Go
package main
import (
"fmt"
)
func main() {
slice_of_slices := make([][]int , 3)
for i := 0; i < 3; i++ {
new_length := i * 2 + 1
slice_of_slices[i] = make([]int, new_length)
for j := 0; j < new_length; j++{
slice_of_slices[i][j] = i * j + 1
}
}
for i := 0; i < 3; i++ {
fmt.Println(slice_of_slices[i])
}
fmt.Println("Slice of slices: ", slice_of_slices)
}
|
Output:
[1]
[1 2 3]
[1 3 5 7 9]
Slice of slices: [[1] [1 2 3] [1 3 5 7 9]]
Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
15 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...