In this tutorial, we will go through some examples of concatenating two
or multiple slices in Golang. We will use the append() function, which
takes a slice as the first argument and the values to append as the
second.
func append(slice []Type, elems ...Type) []Type: The append built-in
function appends elements to the end of a slice. If it has sufficient
capacity, the destination is resliced to accommodate the new elements.
If it does not, a new underlying array will be allocated. Append returns
the updated slice. It is therefore necessary to store the result of
append, often in the variable holding the slice itself:
slice = append(slice, elem1, elem2)
slice = append(slice, anotherSlice...)
We need to use the variadic function signature dots to merge 2 slices. In variadic functions in Golang, we can pass a variable number of arguments to the function and all the numbers can be accessed with the help of the argument name.
Example 1: Merge slices using append() function
Let’s consider a simple example, where we will append an integer to an
integer slice using append() function
package main
import "fmt"
func main() {
intSlice := []int{2, 4, 6}
fmt.Println("Slice before append:", intSlice)
intSlice = append(intSlice, []int{8, 10}...)
fmt.Println("Slice after append:", intSlice)
//new integer slice
intSliceNew := []int{1, 3, 5}
intSlice = append(intSlice, intSliceNew...)
fmt.Println("Slice after 2 append:", intSlice)
}
Output:
Slice before append: [2 4 6]
Slice after append: [2 4 6 8 10]
Slice after 2 append: [2 4 6 8 10 1 3 5]
Example 2: Merge slices using copy() function
If you have the target slice, “appending” multiple slices is simply copying them to the target in the proper position. You can do this by using the built-in copy() function.
func copy(dst, src []Type) int: The copy built-in function copies
elements from a source slice into a destination slice. (As a special
case, it also will copy bytes from a string to a slice of bytes.) The
source and destination may overlap. Copy returns the number of elements
copied, which will be the minimum of len(src) and len(dst).
Let’s consider below example, where we append an integer to a slice
using copy() function
package main
import "fmt"
func main() {
// create 2 slice we want to merge
intSlice1 := [5]int{2, 4, 6, 8, 10}
intSlice2 := [4]int{3, 5, 7, 9}
// create a destination slice
var mergeSlice [len(intSlice1) + len(intSlice2)]int
// copy all elements from slice 1 to destination slice
copy(mergeSlice[:], intSlice1[:])
// copy all elements from slice 2 to destination slice
copy(mergeSlice[len(intSlice1):], intSlice2[:])
fmt.Printf("%v\n", mergeSlice)
}
Output:
[2 4 6 8 10 3 5 7 9]
It should be noted that we can only use this method to append two fixed-length slices.
Example 3: Concatenate multiple slices using append() function
In this example below, we demonstrate how to concatenate multiple string slices.
package main
import "fmt"
func main() {
strSlice := []string{"This", "is", "Go", "Linux", "Cloud!"}
fmt.Println("Slice before append:", strSlice)
strSliceAppend1 := []string{"Welcome", "to", "our", "page."}
strSliceAppend2 := []string{"Have", "a", "nice", "day!"}
//append 2 string slices
strSlice = append(strSlice, strSliceAppend1...)
//append 2 string slices
strSlice = append(strSlice, strSliceAppend2...)
fmt.Println("Slice after append:", strSlice)
}
Output:
Slice before append: [This is Go Linux Cloud!]
Slice after append: [This is Go Linux Cloud! Welcome to our page. Have a nice day!]
You cannot concatenate more than two slices at a time with append. If
you try to do so, it will throw atoo many arguments to append error
at compile time. If you want to concatenate multiple slices, you have to
call the append function multiple times.
Summary
We have shown 3 examples of combining 2 or more slices using copy()
and append() function in this article. We can also concatenate
multiple slices using golang append function but the limitation is that
at a time we can merge only 2 slices so we need to use append function
multiple times to merge individual slice elements.
Reference
https://pkg.go.dev/builtin#append
https://en.wikipedia.org/wiki/Variadic_function
https://pkg.go.dev/builtin#copy

![Golang concatenate or merge two or more slices [SOLVED]](/merge-slices-golang/golang_merge_slices.jpg)
