DEV Community

Cover image for How to add or push new elements to a slice or an array in Go or Golang?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

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

#go

Originally posted here!

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] βœ…
}
Enter fullscreen mode Exit fullscreen mode

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"}
}
Enter fullscreen mode Exit fullscreen mode

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")
}
Enter fullscreen mode Exit fullscreen mode

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] βœ…
}
Enter fullscreen mode Exit fullscreen mode

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 πŸ˜ƒ.


Top comments (2)

Collapse
 
alxizr profile image
alxizr

Seriously mate ??? Seriously?!

Collapse
 
melvin2016 profile image
MELVIN GEORGE

What is it? @alxizr