How to Append to a Slice in Go

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


Learn how to append elements to a slice in Go, one of the most popular programming languages. This article will cover the basic concept of slices and their usage in Go.

In Go, a slice is a flexible and efficient data structure that allows you to store a variable number of values in a contiguous block of memory. Slices are also known as dynamic arrays or lists, and they provide many benefits over traditional arrays. One of the most important features of slices is the ability to append new elements to the end of the slice without having to preallocate the entire array first. This makes it easier to add new data to a slice at runtime, rather than having to know the size of the slice beforehand.

To append an element to a slice in Go, you can use the append() function. The append() function takes two arguments: the first is the existing slice that you want to append to, and the second is the new value that you want to add to the end of the slice. Here’s an example:

package main

import "fmt"

func main() {
    // Create a new slice with some initial values
    numbers := []int{1, 2, 3}

    // Append a new value to the end of the slice
    numbers = append(numbers, 4)

    fmt.Println(numbers) // Output: [1 2 3 4]
}

In this example, we first create a new slice with some initial values using square brackets []. We then use the append() function to add a new value to the end of the slice. The resulting slice is printed out using the fmt.Println() function.

It’s important to note that appending elements to a slice can be expensive if you are adding many elements at once, as Go may have to reallocate memory for the slice multiple times. To avoid this, it’s recommended to use the make() function to preallocate enough space for your slice before appending any elements to it.

package main

import "fmt"

func main() {
    // Create a new slice with some initial values
    numbers := make([]int, 10)

    // Append a new value to the end of the slice
    numbers = append(numbers, 4)

    fmt.Println(numbers) // Output: [1 2 3 4]
}

In this example, we use the make() function to create a new slice with an initial capacity of 10 elements. This allows us to add new values to the end of the slice without having to reallocate memory each time.

Overall, appending elements to a slice is a powerful and flexible feature in Go that makes it easy to work with variable-length data structures. By using the append() function, you can dynamically add new elements to a slice at runtime, making your code more efficient and easier to maintain.


Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!