How to use append() in Golang

In Go, we can append new elements to a slice and remove elements from the slice using a built-in append() function.

func append(slice []T, values ...T) []T

Parameters

  • T: This is a slice with the arbitrary values of the type T to append.

Return value

  • A slice of type T

The resulting value of the append is a slice containing all the elements of the original slice plus the provided values.

Let’s look at a simple example of append() in action.

Append element(s) at the end

package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5};
fmt.Print("Slice before append: ");
fmt.Println(slice)
slice = append(slice, 10);
fmt.Print("Slice after append: ");
fmt.Println(slice)
}
Simple use of `append()`

That was quite simple. The value got inserted at the end of the slice. But how do we add an element to the beginning of the slice or at a specific index in the slice?

The beauty of Go is that it forces a programmer to think like a programmer. In other languages, there're different functions for inserting at the start, at the end, and at the specific index. But, in Go we can use the same append function to do all of that.

Append element(s) at the start

package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5};
fmt.Print("Slice before append: ");
fmt.Println(slice)
slice = append([]int{10}, slice...);
fmt.Print("Slice after append: ");
fmt.Println(slice)
}
How to `append()` the slice in start
  • Line 11: We have only changed this line and, as we know that the first argument of the append is a slice. We have converted the int to []int.

Notice, the ... at the end of the slice. The second parameter of the append is a variadic parameter. A variadic parameter accepts zero or more values of a specified type.

From the Go Spec: If f is variadic with final parameter type ...T, then within the function the argument is equivalent to a parameter of type []T. At each call of f, the argument passed to the final parameter is a new slice of type []T whose successive elements are the actual arguments, which all must be assignable to the type T.

Append element at a specific index

package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
slice := []int{1, 2, 3, 4, 5};
fmt.Print("Slice before append: ");
fmt.Println(slice)
rand.Seed(time.Now().UnixNano())
idx := rand.Intn(len(slice))
fmt.Println("Index:", idx)
tempSlice := append(slice[:idx], 10)
slice = append(tempSlice, slice[idx + 1:]...)
fmt.Print("Slice after append: ");
fmt.Println(slice)
}
How to `append()` the slice at specific index

To append an element at a specific index we have to use slicing.

  • Line 19: Creating a new slice with all the values from start index (0) to the index idx and added the new element to the end of it.
  • Line 20: Using the tempSlice and appending the remaining slice at the end of it.

This solution works even if we want to insert at the start or end. These scenarios cover all that is to appending a new element to a slice. Imagine if we can even remove/delete an element using the same append function?

Here's an example:

Delete element from the end

package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5};
fmt.Print("Slice before append: ");
fmt.Println(slice)
slice = append(slice[: len(slice)-1]);
fmt.Print("Slice after append: ");
fmt.Println(slice)
}
How to delete index using `append()`

We can also delete from the start or a specific index using the techniques discussed above.

Free Resources