A slice is a bit like a dynamic array in other languages. Unlike a standard Go Array, slices can have items added and removed items from them as shown in the example below.
To create an empty slice to hold a list of strings, you can do the following. Note how we do not include any size:
planets := []string{}
To add items to the slice, we use the built-in 'append' function:
planets = append(planets, "Moon")
planets = append(planets, "Mercury")
planets = append(planets, "Mars")
// Here we add two items at once
planets = append(planets, "Jupiter", "Venus")
Just like with arrays, we can use a for loop to read through the values:
// Each value is taken from the planets slice and put into the 'v' variable.
for _, v := range planets {
fmt.Println(v)
}
Removing is a little trickier, we can the Delete function from the 'slices' package and give it a range to delete. In the block below, start is the first item we want to delete, end is one higher than the last item to delete e.g.
// If our slice contains:
// 0 = Moon
// 1 = Mercury
// 2 = Mars
// 3 = Jupiter
// 4 = Venus
// To delete items 2 and 3 we would do the following:
planets = slices.Delete(planets, 2, 4)
The Go compiling guide explains the folder set up for a Go program, once that is done:
package main
import (
"fmt"
"slices"
)
func main() {
planets := []string{}
// Adding to the 'planets' slice
planets = append(planets, "Moon")
planets = append(planets, "Mercury")
planets = append(planets, "Mars")
planets = append(planets, "Jupiter", "Venus")
// Show our planets (and moon)
for _, v := range planets {
fmt.Println(v)
}
// Delete Mars and Jupiter, items 2 and 3
planets = slices.Delete(planets, 2, 4)
// Show our planets (and moon)
for _, v := range planets {
fmt.Println(v)
}
}
Updated (25/08/2026)