Last modified: Jan 21, 2026 By Alexander Williams

Go List of Strings: Create, Iterate, Manipulate

Go is a powerful language for building fast software. A common task is handling collections of text. In Go, we use slices to create a list of strings.

This guide covers everything you need. You will learn how to create, modify, and work with string lists.

What is a Slice in Go?

Go does not have a built-in "list" type like some languages. Instead, it uses slices. A slice is a flexible view into an array.

Think of a slice as a dynamic list. It can grow and shrink as needed. This makes it perfect for managing a collection of strings.

If you are new to Go, start with Your First Go Program: Hello World Tutorial. It covers the basics.

Creating a List of Strings

You can create a string slice in several ways. The most common is using a slice literal.


package main

import "fmt"

func main() {
    // Method 1: Slice literal
    fruits := []string{"apple", "banana", "cherry"}
    fmt.Println("Fruits:", fruits)

    // Method 2: Using the var keyword
    var colors []string
    colors = []string{"red", "green", "blue"}
    fmt.Println("Colors:", colors)

    // Method 3: Using make (specify length and capacity)
    tools := make([]string, 3) // length 3, capacity 3
    tools[0] = "hammer"
    tools[1] = "screwdriver"
    tools[2] = "wrench"
    fmt.Println("Tools:", tools)
}

Fruits: [apple banana cherry]
Colors: [red green blue]
Tools: [hammer screwdriver wrench]

Iterating Over a String Slice

You often need to loop through each item in your list. Go provides the for loop and the range keyword for this.


package main

import "fmt"

func main() {
    animals := []string{"cat", "dog", "bird", "fish"}

    // Loop with index and value
    fmt.Println("Using index and value:")
    for index, animal := range animals {
        fmt.Printf("Index %d: %s\n", index, animal)
    }

    // Loop with value only
    fmt.Println("\nUsing value only:")
    for _, animal := range animals {
        fmt.Println(animal)
    }

    // Loop with index only
    fmt.Println("\nUsing index only:")
    for i := range animals {
        fmt.Println("Item at position", i)
    }
}

Using index and value:
Index 0: cat
Index 1: dog
Index 2: bird
Index 3: fish

Using value only:
cat
dog
bird
fish

Using index only:
Item at position 0
Item at position 1
Item at position 2
Item at position 3

Common Operations on String Slices

Once you have a list, you will want to modify it. Here are the most useful operations.

Appending Elements

Use the built-in append function to add items. It returns a new slice.


package main

import "fmt"

func main() {
    cities := []string{"London", "Paris"}
    fmt.Println("Original:", cities)

    // Append one item
    cities = append(cities, "Tokyo")
    fmt.Println("After adding Tokyo:", cities)

    // Append multiple items
    cities = append(cities, "New York", "Berlin")
    fmt.Println("After adding two more:", cities)

    // Append another slice
    moreCities := []string{"Sydney", "Cairo"}
    cities = append(cities, moreCities...)
    fmt.Println("After appending a slice:", cities)
}

Original: [London Paris]
After adding Tokyo: [London Paris Tokyo]
After adding two more: [London Paris Tokyo New York Berlin]
After appending a slice: [London Paris Tokyo New York Berlin Sydney Cairo]

Sorting a List of Strings

Sorting requires the sort package. Use sort.Strings.


package main

import (
    "fmt"
    "sort"
)

func main() {
    names := []string{"Zoe", "Alice", "Bob", "Maya"}
    fmt.Println("Before sort:", names)

    sort.Strings(names) // Sorts in-place
    fmt.Println("After sort:", names)

    // Check if a slice is already sorted
    isSorted := sort.StringsAreSorted(names)
    fmt.Println("Is the slice sorted?", isSorted)
}

Before sort: [Zoe Alice Bob Maya]
After sort: [Alice Bob Maya Zoe]
Is the slice sorted? true

Filtering and Searching

Go does not have built-in filter or find functions. You write them yourself with loops.


package main

import (
    "fmt"
    "strings"
)

func main() {
    words := []string{"go", "golang", "code", "program", "google", "game"}

    // Filter: Find words starting with "go"
    var filtered []string
    for _, w := range words {
        if strings.HasPrefix(w, "go") {
            filtered = append(filtered, w)
        }
    }
    fmt.Println("Words starting with 'go':", filtered)

    // Search: Find index of "code"
    target := "code"
    index := -1
    for i, w := range words {
        if w == target {
            index = i
            break
        }
    }
    fmt.Printf("The word '%s' is at index %d\n", target, index)
}

Words starting with 'go': [go golang google game]
The word 'code' is at index 2

Converting a String to a Slice (and Back)

Sometimes you need to split a string or join a slice into one string.


package main

import (
    "fmt"
    "strings"
)

func main() {
    // Split a string into a slice
    data := "apple,banana,cherry"
    fruitSlice := strings.Split(data, ",")
    fmt.Println("Split result:", fruitSlice)
    fmt.Println("First fruit:", fruitSlice[0])

    // Join a slice into a string
    newString := strings.Join(fruitSlice, " | ")
    fmt.Println("Joined string:", newString)

    // Convert a string to a slice of runes or bytes
    greeting := "Hello"
    byteSlice := []byte(greeting)
    runeSlice := []rune(greeting)
    fmt.Println("Byte slice:", byteSlice)
    fmt.Println("Rune slice:", runeSlice)
}

Split result: [apple banana cherry]
First fruit: apple
Joined string: apple | banana | cherry
Byte slice: [72 101 108 108 111]
Rune slice: [72 101 108 108 111]

Best Practices and Performance

Use these tips to write better Go code with string slices.

Pre-allocate capacity. If you know the final size, use make with a capacity. This prevents repeated memory allocations when using append.

Prefer slices over arrays. Slices are more common and flexible. Arrays have a fixed size.

Use the blank identifier. Use _ when you don't need the index or value in a range loop.

Managing dependencies is key. Learn how with our Understanding go.mod: Go Modules & Versioning Guide.

Conclusion

String slices are a fundamental data structure in Go. You now know how to create, iterate, and manipulate them.

You learned to append, sort, filter, and convert data. These skills are vital for handling text in any Go application.

Remember to use the right tools. Check out the Essential Go Tooling Guide to master commands like go run and go test.

Start practicing with your own lists of strings. Experiment with the examples. You will quickly become proficient.