How to add or push new elements to a slice or an array in Go or Golang?

October 4, 2022 - 3 min read

To add or push new elements to an array or slice, you can use the append() built-in function and then pass the slice as the first argument and the values to add to the slice as the following arguments. The append() function returns a new slice with the newly added elements.

TL;DR

package main

import "fmt"

func main() {
    // a slice that
    // contains people names
    names := []string{"John Doe", "Lily Roy", "John Daniels"}

    // add a new item/name to the `names` slice
    // using the built-in `append()` function
    // and pass the `names` variable as the first
    // argument and the string of `Roy Daniels`
    // as the second argument.
    // the `append()` function returns a
    // new slice with the newly added item/name.
    names = append(names, "Roy Daniels")

    // log the values of the `names` slice
    fmt.Println(names) // [John Doe Lily Roy John Daniels Roy Daniels] ✅
}

For example, let's say we have a string type slice that contains some names of people like this,

package main

func main(){
    // a slice that
    // contains people names
    names := []string{"John Doe", "Lily Roy", "John Daniels"}
}

Now to add a new name called "Roy Daniels" we can use the append() built-in function and pass the names slice variable as the first argument and the string of "Roy Daniels" as the second argument.

It can be done like this,

package main

func main(){
    // a slice that
    // contains people names
    names := []string{"John Doe", "Lily Roy", "John Daniels"}

    // add a new item/name to the `names` slice
    // using the built-in `append()` function
    // and pass the `names` variable as the first
    // argument and the string of `Roy Daniels`
    // as the second argument.
    // the `append()` function returns a
    // new slice with the newly added item/name.
    names = append(names, "Roy Daniels")
}

Now to check if the new name got added to the names slice, we can print all the values of the names slice into the console using the Prinln() method from the fmt package.

It can be done like this,

package main

import "fmt"

func main() {
    // a slice that
    // contains people names
    names := []string{"John Doe", "Lily Roy", "John Daniels"}

    // add a new item/name to the `names` slice
    // using the built-in `append()` function
    // and pass the `names` variable as the first
    // argument and the string of `Roy Daniels`
    // as the second argument.
    // the `append()` function returns a
    // new slice with the newly added item/name.
    names = append(names, "Roy Daniels")

    // log the values of the `names` slice
    fmt.Println(names) // [John Doe Lily Roy John Daniels Roy Daniels] ✅
}

As you can see at the end of the output in the slice, we can see our new name Roy Daniels got added which proves that our new item got added to the slice. Yay 🥳.

See the above code live in The Go Playground.

That's all 😃!

Feel free to share if you found this useful 😃.