Golang Program to Implement Slices


A slice in golang is a dynamic array created to add extra elements which cannot be added in an array as it has a fixed size. In this particular article, two examples are used to demonstrate the use of slices. In both of the examples, various operations are performed on the slices to show its working. In the first example, a slice of string is created and in the second example, slice of integers is created. Let’s see the operations via examples to get a crystal-clear understanding of the concept.

Syntax

funccopy(dst, str[] type) int

The copy function in go language is used to copy the values of one source array to the destination array and returns the number of elements copied as the result. It takes the two arrays as an argument.

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 main function and in that function create a slice of string elements using make function which is a type of built-in function

  • In this step, set the values in the slice using indexing, set three values in the slice

  • Print the slice elements on the console using Println function from fmt package

  • Then, append the values in the slice using append function which is also a built-in function in Golang

  • Print the updated slice on the console in the same way as did in previous steps

  • Then, slice the slice and print it on the console

  • Finally, iterate the slice of chocolates till its range and in each iteration print the index and the value at that index using Println function where ln means new line

Example 1

In this example, we will create a slice of type string and manipulate the slice by first setting the values in the slice, appending new values in it, slicing the slice and then iterating it. All the outputs will be printed using the fmt package.

package main

import "fmt"

func main() {
   // create a slice with an initial capacity of 3
   slice := make([]string, 3)
   fmt.Println("empty slice:", slice)

   // set values to the slice
   slice[0] = "munch"
   slice[1] = "bourbon"
   slice[2] = "perk"
   fmt.Println("set slice:", slice)

   slice = append(slice, "Dairy milk")
   fmt.Println("appended slice:", slice)

   sliced := slice[1:4]
   fmt.Println("sliced slice:", sliced)

   // iterate over the slice
   for i, chocolates := range slice {
      fmt.Printf("index %d, value %s\n", i, chocolates)
   }
}

Output

empty slice: [  ]
set slice: [munch bourbon perk]
appended slice: [munch bourbon perk Dairy milk]
sliced slice: [bourbon perk Dairy milk]
index 0, value munch
index 1, value bourbon
index 2, value perk
index 3, value Dairy milk

Example 2

In this example, a slice will be created like in the previous example but here integers will be set in the slice, then it will be manipulated like a slice will be sliced, then elements will be added in slice using built-in functions and a new copied slice will be created.

//Golang program to implement slices
package main

import "fmt"

func main() {
   slice := []int{10, 20, 30, 40, 50}
   fmt.Println("initial slice:", slice)

   sliced := slice[1:4]
   fmt.Println("sliced slice:", sliced)

   slice[2] = 11
   slice[4] = 22
   fmt.Println("modified slice:", slice)

   sliced[1] = 44
   fmt.Println("modified sliced slice:", sliced)

   slice = append(slice, 66)
   fmt.Println("appended slice:", slice)
   
   copySlice := make([]int, len(slice))
   copy(copySlice, slice)
   fmt.Println("copied slice:", copySlice)
}

Output

initial slice: [10 20 30 40 50]
sliced slice: [20 30 40]
modified slice: [10 20 11 40 22]
modified sliced slice: [20 44 40]
appended slice: [10 20 44 40 22 66]
copied slice: [10 20 44 40 22 66]

Conclusion

We executed and compiled the program of implementing the slices using two examples. In both the examples we demonstrated the use of slices by inserting new elements, copying the slice, slicing the slice.

Updated on: 20-Jul-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements