How to Append a Slice in Golang

To append a slice in Golang, use the append() function. The append() is a built-in Go function used to append elements to a slice. As per the official Go documentation, the following is the signature of the append() function from the built-in package.

As we know, arrays in Go are statically-sized, which means once created, their size cannot be changed later. However, unlike arrays, slices in go are dynamically-sized so that we can add new elements to a slice. This tutorial will explore how to add elements to a Go slice.

How does append() work in Golang

The append() doesn’t modify the original slice but creates a new slice, copies all the elements from the original slice, and puts the provided elements at the end.

Also, the length of the slice is updated, and depending on the capacity of the slice, resizing might be required.

When slice length is less than capacity, appending 1 element increases the length by 1 of the slice, but the capacity is not changed.

When the slice length is equal to its capacity, it cannot accommodate new elements. So, in this case, a new array with a capacity double that of the underhood array will be allocated, and the pointer in the slice will be changed to point to the new array. So, capacity is doubled, but length increases only by 1.

Let’s see the syntax of the append() function.

Syntax

func append(slice []Type, elems ...Type) []Type

Arguments

The append() is a variadic function(function with a variable number of arguments) that takes the following arguments

  1. slice: The slice to be appended.
  2. elems: One or more(variable) elements of the same type as the slice.

Return value

The return value is the final slice containing all the elements appended at the end of the original slice.

Note: The append() copies the elements and creates a new slice. The original slice is not modified.

Example

package main

import "fmt"

func main() {
  s := []int{} // empty slice

  s = append(s, 1) // appending 1 element to empty slice => s = [1]
  fmt.Println(s)

  s = append(s, 2) // appending 1 element to non-empty slice => s = [1, 2]
  fmt.Println(s)

  s = append(s, 3, 4) // appending multiple elements => s = [1, 2, 3, 4]
  fmt.Println(s)
}

Output

[1]
[1 2]
[1 2 3 4]

First, we defined an empty slice, and then we appended elements one by one, and the final output is a slice with four elements.

How to append one slice to another in Go

To append all the elements of one slice to another in Golang, use the triple dot(…) operator. The triple dot operator(…) is a built-in Go operator that concatenates slices. Since append() is a variadic function, we can use the notation.

package main

import "fmt"

func main() {
 s := []int{1, 2}
 s2 := []int{3}

 s = append(s, s2...) // appending one slice to another
 fmt.Println(s)
}

Output

[1 2 3]

We concatenate the two slices(s, s2) using the … operator in Golang.

How to append a String in Golang

To append a string to a slice of bytes in Golang, use the triple dot(…) operator. A string in Go is nothing but a sequence of bytes.

package main

import "fmt"

func main() {
  slice := []byte("Good") // byte slice
  str := " Morning"

  slice = append(slice, str...) // appending string
  fmt.Println(string(slice))

  // appending constant string, note the ... operator
  slice = append(slice, ", Mumbai"...)
  fmt.Println(string(slice))
}

Output

Good Morning 
Good Morning, Mumbai

You can see that we appended a string(str) to a slice of bytes(slice) using the triple dot(…) operator.

That’s it for this tutorial.

See also

How to Convert String to Byte Array in Golang

How to Convert Byte Array to String in Golang

How to Create a Byte Array in Golang

Leave a Comment