Golang program to merge two slices


In Golang, a slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes 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 array containing all the values.

func make ([] type, size, capacity)

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

func copy(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.

Algorithm

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

  • Step 2 − Create a function main and in that function create a slice1 and fill it with some values.

  • Step 3 − Create another slice2 and also fill it with some values so that it can merged with slice1.

  • Step 4 − Use append function to merge slice1 and slice2 in slice3 and print this new slice on the console.

  • Step 5 − Here .. signifies that the elements of slice2 are passed as individual arguments instead of a single slice.

  • Step 6 − The print statement is executed on the console using fmt.Println() function where ln means new line.

Example 1

In this example, we will learn how to merge two slices using append function which is a type of built-in function. Two slice will be created and they will be appended to one slice which will be printed on the console. Let’s understand it through via the algorithm and the code.

package main
import "fmt"
func main() {
   slice1 := []int{10, 20, 30} //create slice1
   fmt.Println("The elements of slice1 are:", slice1)
   slice2 := []int{40, 50, 60} //create slice2
   fmt.Println("The elements of slice2 are:", slice2)
   slice3 := append(slice1, slice2...) //append slice1 and slice2
   fmt.Println(slice3) //print the slice3 which contains both slice1 and slice2
}

Output

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [40 50 60]
[10 20 30 40 50 60]

Example 2

In this example, we will learn how to merge two slices using copy function. Here we will create two slices and copy their elements in another slice using built-in function. Let’s go through the algorithm and the code to understand it.

package main
import "fmt"
func main() {
   myslice1 := []int{10, 20, 30} //create slice1
   fmt.Println("The elements of slice1 are:", myslice1)
   myslice2 := []int{40, 50, 60} //create slice2
   fmt.Println("The elements of slice2 are:", myslice2)
   myslice3 := make([]int, len(myslice1)+len(myslice2))
   copy(myslice3, myslice1)
   copy(myslice3[len(myslice1):], myslice2)
   fmt.Println("The elements of slice3 after the elements of above two slices merged are:")
   fmt.Println(myslice3) //print new slice with elements of both slice1 and slice2
}

Output

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [40 50 60]
The elements of slice3 after the elements of above two slices merged are:
[10 20 30 40 50 60]

Example 3

In this example we will use an append function in for loop to merge two slices. The two slices will be created in this example and in one the elements will be merged. Let’s dive into the algorithm and the code to see how it’s done.

package main
import "fmt"
func main() {
   myslice1 := []int{10, 20, 30} //create slice1
   fmt.Println("The elements of slice1 are:", myslice1)
   myslice2 := []int{40, 50, 60} //create slice2
   fmt.Println("The elements of slice2 are:", myslice2)
   fmt.Println("The slice after its elements merged are:")
   for _, element := range myslice2 {
      myslice1 = append(myslice1, element) //using append function append slices
   }
   fmt.Println(myslice1) //print slice1
}

Output

The elements of slice1 are: [10 20 30]
The elements of slice2 are: [40 50 60]
The slice after its elements merged are:
[10 20 30 40 50 60]

Conclusion

We executed the program of merging two slices using three examples. In the first example we used append function to merge two slices. In the second example we used copy function to merge the slices and in the third example we used for loop with append function. Both the functions are built-in functions. Hence, the program executed successfully.

Updated on: 13-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements