Golang Program to Create Slice of Slices


A slice in go language is a variable length array which means that values can be added and deleted from it as per convenience. In this article, we will create a slice of slices using two examples, slice of slices means that many slices are contained in a slice. In the first example, we will demonstrate the creation of slice of slices in two ways, firstly we will initiate the slice of slices with some values and in the second way an empty slice of slices will be created in which values will be appended later on. In the second example, an empty slice of slices will be created using the make function and then values will be appended in it to get the output.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map that accepts the type of variable to be created, its size and capacity as arguments.

funcappend(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes a number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of the array containing all the values.

Algorithm

  • Create a package main and declare the fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Create a main function and in that function create a slice of slices of type integer

  • Initialize the slice with some values and print the slice of slices on the console using Println function where ln means new line

  • Then, create an empty slice of slices of type integer and append values in the slice of slices using append method which is a built-in function in Golang

  • Then, print the empty_slice on the console similarly like we did in last steps

Example 1

In this example, we will create a main and in that main we will create a slice of slices and add values in the slice of slices, and in the second way we will create an empty slice and append values in the empty slice using append method. In this way we will demonstrate how to create slice of slices.

package main

import "fmt"

//Main function to execute the program
func main() {
   slice_of_slices := [][]int{
      []int{10, 20, 30},
      []int{40, 50, 60},
      []int{70, 80, 90},
   }

   // Print the slice of slices
   fmt.Println("The slice of slices is:")
   fmt.Println(slice_of_slices)

   // Create an empty slice of slices and append slices to it
   empty_slice := [][]int{}
   empty_slice = append(empty_slice, []int{1, 10, 2})
   empty_slice = append(empty_slice, []int{3, 4, 5})
   empty_slice = append(empty_slice, []int{6, 7, 8})

   // Print the empty slice of slices with appended slices
   fmt.Println(empty_slice)
}

Output

The slice of slices is:
[[10 20 30] [40 50 60] [70 80 90]]
[[1 10 2] [3 4 5] [6 7 8]]

Example 2

In this example, in the main function an empty slice of slices will be created in which values will be added through the use of different slices with the append method. The output will be the slice of slices printed on the console using the fmt package.

package main

import "fmt"

func main() {
   // Create an empty slice of slices
   slice_of_slices := make([][]int, 0)

   slice1 := []int{10, 20, 30}
   slice2 := []int{40, 50, 60}
   slice3 := []int{70, 80, 90}
   slice_of_slices = append(slice_of_slices, slice1)
   slice_of_slices = append(slice_of_slices, slice2)
   slice_of_slices = append(slice_of_slices, slice3)

   // Print the slice of slices on the console
   fmt.Println("The slice of slices is:")
   fmt.Println(slice_of_slices)
}

Output

The slice of slices is:
[[10 20 30] [40 50 60] [70 80 90]]

Conclusion

We executed and compiled the program of creating slice of slices using two examples. In the first example we created a slice of slices, added values in it and further created an empty slice in which we added values. Then, in the second example we created an empty slice and added values in it individually.

Updated on: 20-Jul-2023

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements