How to append a slice to another slice in golang?
April 04, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
Today, we will learn to append slice to a another slice in golang, here we will use append()
golang build in method.
So we will create two slices like following example.
package main
import "fmt"
func main() {
x, y := []int{1, 2}, []int{3, 4}
z := append(x, y...)
fmt.Print(z)
}
Here, we have created two slice x
and y
, use append method to concate slice and, store in third variable z
.
Now You see the output here showing z value is [1, 2, 3, 4]
.
Output
All the best 👍