How to Append a Slice in Golang

Slice is an important data structure in Go because it is a variable-length sequence that stores elements of a similar type that are not allowed to store different elements.

What is a slice in Go?

Golang slice is a dynamically-sized, flexible view into the elements of an array. The slice type is an abstraction built on top of the array type. The length of a slice can extend and shrink as you see fit.

How to create a slice?

To create a slice in Go, use the slice_name:= [ ]datatype{values} syntax.

package main

import (
  "fmt"
)

func main() {
  mango_slice := []int{1, 2, 3}
  fmt.Println(mango_slice)
  fmt.Printf("The type of variable is : %T\n", mango_slice)
}

Output

[1 2 3]
The type of variable is : []int

Now, let’s see how to append an element to the slice.

How to append 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 that 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.

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.

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() to a slice of bytes() using the triple dot(…) operator.

That’s it for this tutorial.

Leave a Comment