How to append a slice in Golang?

05 Sep, 2019
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. A slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. It is just like an array having an index value and length, but the size of the slice is resized they are not in fixed-size just like an array.As we already know that slice is dynamic, so the new element can be appended to a slice with the help of append() function. This function appends the new element at the end of the slice.
Syntax:
func append(s []T, x ...T) []T
Here, this function takes s slice and x...T means this function takes a variable number of arguments for the x parameter. Such type of function is also known as a variadic function.
If the slice is backed by the array and arrays are fixed length, then how is that possible a slice is a dynamic length?.
Well, the answer is when the new elements append to the slice a new array is created. Now, the elements present in the existing array are copied into a new array and return a new slice reference to this array(new array). So, due to this, the capacity of the slice is going to be double from the old capacity(As shown in example 1). But if the existing slice has enough capacity to hold new elements, then no new array is created and the elements store in the existing underlying array(As shown in example 2).
Example 1:
// Go program to illustrate the// concept of appending slices.package main  import (    "fmt")  func main() {      // Creating and initializing slice    // Using shorthand declaration    s1 := []int{234, 567, 7890, 1234, 234}    s2 := []string{"Geeks", "For", "Geeks"}      // Displaying slices with    // their length and capacity    fmt.Println("Slice_1: ", s1)    fmt.Println("Length of Slice_1: ", len(s1))    fmt.Println("Capacity of Slice_1: ", cap(s1))    fmt.Println()    fmt.Println("Slice_2: ", s2)    fmt.Println("Length of Slice_2: ", len(s2))    fmt.Println("Capacity of Slice_2: ", cap(s2))      // Appending slices    // Using append() function    res1 := append(s1, 1000)    res2 := append(s2, "GFG")      // Displaying results    fmt.Println()    fmt.Println("New slice_1: ", res1)    fmt.Println("New length of slice_1: ", len(res1))    fmt.Println("New capacity of slice_1: ", cap(res1))    fmt.Println()    fmt.Println("New slice_2: ", res2)    fmt.Println("New length of slice_2: ", len(res2))    fmt.Println("New capacity of slice_2: ", cap(res2))}
Output:
Slice_1:  [234 567 7890 1234 234]
Length of Slice_1:  5
Capacity of Slice_1:  5

Slice_2:  [Geeks For Geeks]
Length of Slice_2:  3
Capacity of Slice_2:  3

New slice_1:  [234 567 7890 1234 234 1000]
New length of slice_1:  6
New capacity of slice_1:  12

New slice_2:  [Geeks For Geeks GFG]
New length of slice_2:  4
New capacity of slice_2:  6

Example 2:
// Go program to illustrate the// concept of appending slices.package main  import "fmt"  func main() {      // Creating and initializing slice    // Using make() function    // Here 4 and 6 is the length    // and capacity of the slice    s1 := make([]int, 4, 6)      // Copying the elements in the slice    // Using copy() function    copy(s1, []int{123, 456, 789, 977})      // Displaying slice    fmt.Println("Slice : ", s1)    fmt.Println("Length: ", len(s1))    fmt.Println("Capacity: ", cap(s1))      // Appending slice    // Using append() function    s2 := append(s1, 3454, 678)      // Displaying slice    fmt.Println()    fmt.Println("Slice : ", s2)    fmt.Println("Length: ", len(s2))    fmt.Println("Capacity: ", cap(s2))  }
Output:
Slice :  [123 456 789 977]
Length:  4
Capacity:  6

Slice :  [123 456 789 977 3454 678]
Length:  6
Capacity:  6

Appending to nil slice: As we know that zero value slice type is nil and the capacity and the length of such type of slice is 0. But with the help of append function, it is possible to append values to nil slice.
Example:
// Go program to illustrate the// concept of appending to nil slice.package main  import "fmt"  func main() {      // Creating nil slice    var s1 []int      // Displaying slice    fmt.Println("Slice : ", s1)    fmt.Println("Length: ", len(s1))    fmt.Println("Capacity: ", cap(s1))      // Appending to nil slice    // Using append() function    s2 := append(s1, 89, 45, 67, 23)      // Displaying slice    fmt.Println()    fmt.Println("Slice : ", s2)    fmt.Println("Length: ", len(s2))    fmt.Println("Capacity: ", cap(s2))  }
Output:
Slice :  []
Length:  0
Capacity:  0

Slice :  [89 45 67 23]
Length:  4
Capacity:  4

Appending to another slice using ... operator: You are allowed to append one slice to another with the help of ... operator.
Example:
// Go program to illustrate the concept // of appending to another slice.package main  import "fmt"  func main() {      // Creating and initializing slice    // Using shorthand declaration    s1 := []int{234, 567, 7890, 1234, 234}    s2 := []int{10, 100, 1000, 10000}      // Displaying slices with their    // length and capacity    fmt.Println("Slice_1: ", s1)    fmt.Println("Length of Slice_1: ", len(s1))    fmt.Println("Capacity of Slice_1: ", cap(s1))          fmt.Println()          fmt.Println("Slice_2: ", s2)    fmt.Println("Length of Slice_2: ", len(s2))    fmt.Println("Capacity of Slice_2: ", cap(s2))      // Appending to another slice    // Using append() function    res1 := append(s1, s2...)      // Displaying result    fmt.Println()    fmt.Println("New slice_1: ", res1)    fmt.Println("New length of slice_1: ", len(res1))    fmt.Println("New capacity of slice_1: ", cap(res1))  }
Output:
Slice_1:  [234 567 7890 1234 234]
Length of Slice_1:  5
Capacity of Slice_1:  5

Slice_2:  [10 100 1000 10000]
Length of Slice_2:  4
Capacity of Slice_2:  4

New slice_1:  [234 567 7890 1234 234 10 100 1000 10000]
New length of slice_1:  9
New capacity of slice_1:  12

Golang
Golang-Slices
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Golang Maps
Arrays in Go
How to copy one slice into another slice in Golang?
Time Durations in Golang
Interfaces in Golang
Inheritance in GoLang
Check if the String starts with specified prefix in Golang
How to iterate over an Array using for loop in Golang?
How to compare two slices of bytes in Golang?
Type Casting or Type Conversion in Golang