How to append a slice to another slice in golang?

Hi 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

Golang, append, slice, example

Try it yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts