How to add or push new elements to a slice or an array in Go or Golang?
Tags: go
Published: 2022-10-03 19:52:56

---
  title: How to add or push new elements to a slice or an array in Go or Golang?
  published: true
  tags: Go
  date: Tue Oct 04 2022 01:22:56 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/add-or-push-new-elements-to-slice-or-array-go-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-bc32b4335efca2d0a9b4b7fed1d57c78.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/add-or-push-new-elements-to-slice-or-array-go-golang)
  
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

```go
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,

```go
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,

```go
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,

```go
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](https://go.dev/play/p/bZpC3PflGBg).

That's all 😃!

### Feel free to share if you found this useful 😃.

---