Skip to content
Related Articles

Related Articles

Improve Article
Slice of Slices in Golang
  • Last Updated : 25 Aug, 2020

In Go language, 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 store elements on the only a similar 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"
)
  
// main function
func main() {
      
    // declaring a slice of slices of 
    // type integer with a length of 3
    slice_of_slices := make([][]int , 3)
      
    for i := 0; i < 3; i++ {
        // looping through the slice to declare
        // slice of slice of length 3
        slice_of_slices[i] = make([]int, 3)
          
        // assigning values to each 
        // slice of a slice
        for j := 0; j < 3; j++{
            slice_of_slices[i][j] = i * j
        }
    }
      
    // printing the slice of slices matrix
    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"
)
  
// main function
func main() {
      
    // declaring a slice of slices of 
    // type integer with a length of 3
    slice_of_slices := make([][]int , 3)
      
    for i := 0; i < 3; i++ {
          
        new_length := i * 2 + 1
        // looping through the slice to declare
        // slice of slice of a variable length
        slice_of_slices[i] = make([]int, new_length)
          
        // assigning values to each 
        // slice of a slice
        for j := 0; j < new_length; j++{
            slice_of_slices[i][j] = i * j + 1
        }
    }
      
    // printing the slice of slices matrix
    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]]




My Personal Notes arrow_drop_up
Recommended Articles
Page :