Last updated on Sep 16, 2021 by Suraj Sharma
In this tutorial, you’ll learn how you can concatenate two slices of the same type in Go.
Go language provides a built-in function append()
to combine two or more slices of the same type.
append()
function accepts two or more arguments, it returns a new slice
newSlice := append([]int{1,2,3}, 4, 5)
// []int{1,2,3,4,5}
To merge two slices, we use the …
operator as a postfix to the second slice
package main
import "fmt"
func main() {
slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6, 7}
slice3 := append(slice1, slice2...)
fmt.Println(slice3)
}
Related Solutions
Rate this post
Suraj Sharma is the founder of Future Gen AI Services. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.