Golang slices.AppendSeq
last modified April 20, 2025
This tutorial explains how to use the slices.AppendSeq function in Go.
We'll cover slice operations with practical examples of appending elements.
The slices.AppendSeq function appends multiple elements to a slice in a single operation. It's part of Go's experimental slices package.
This function provides a convenient way to add several elements at once while handling memory allocation efficiently. It returns a new slice with all elements.
Basic slices.AppendSeq Example
The simplest use of slices.AppendSeq appends numbers to a slice.
We start with an initial slice and add multiple values.
package main
import (
"fmt"
"slices"
)
func main() {
numbers := []int{1, 2, 3}
numbers = slices.AppendSeq(numbers, 4, 5, 6)
fmt.Println("Appended numbers:", numbers)
}
We create a slice with three numbers and append three more values. The function returns a new slice containing all six elements in order.
Appending Different Types
slices.AppendSeq works with any slice type. This example appends
strings to a string slice.
package main
import (
"fmt"
"slices"
)
func main() {
fruits := []string{"apple", "banana"}
fruits = slices.AppendSeq(fruits, "cherry", "date", "elderberry")
fmt.Println("Fruits:", fruits)
}
The function appends three string elements to our initial two-element slice. Type safety is maintained as all elements must match the slice type.
Appending to Empty Slice
We can use slices.AppendSeq with empty slices. This example starts
with nil and builds a slice.
package main
import (
"fmt"
"slices"
)
func main() {
var empty []float64
numbers := slices.AppendSeq(empty, 1.1, 2.2, 3.3)
fmt.Println("Created from empty:", numbers)
}
Starting with a nil slice, we create a new slice with three float values. The function handles the initial allocation automatically.
Combining Multiple Slices
slices.AppendSeq can combine with the spread operator to merge
slices. This example demonstrates merging two slices.
package main
import (
"fmt"
"slices"
)
func main() {
part1 := []int{1, 2, 3}
part2 := []int{4, 5, 6}
combined := slices.AppendSeq(part1, part2...)
fmt.Println("Combined slices:", combined)
}
Using the spread operator (...), we append all elements from part2 to part1. This creates a new slice containing all elements from both slices.
Appending Struct Values
We can append struct instances using slices.AppendSeq. This example
works with a custom Person type.
package main
import (
"fmt"
"slices"
)
type Person struct {
Name string
Age int
}
func main() {
people := []Person{{"Alice", 25}}
people = slices.AppendSeq(people, Person{"Bob", 30}, Person{"Charlie", 17})
fmt.Println("People:", people)
}
We start with one person and append two more. The function handles the struct values just like primitive types, maintaining type safety.
Performance Comparison
This example compares slices.AppendSeq with multiple append calls.
We measure the performance difference.
package main
import (
"fmt"
"slices"
"time"
)
func main() {
const count = 1_000_000
var base []int
// Using slices.AppendSeq
start := time.Now()
base = slices.AppendSeq(base, make([]int, count)...)
fmt.Println("AppendSeq duration:", time.Since(start))
// Using multiple append calls
start = time.Now()
for i := 0; i < count; i++ {
base = append(base, i)
}
fmt.Println("Multiple append duration:", time.Since(start))
}
slices.AppendSeq is generally faster for bulk operations as it can
allocate memory more efficiently than multiple individual append calls.
Practical Example: Building a Command
This practical example builds a command string from parts using
slices.AppendSeq.
package main
import (
"fmt"
"slices"
"strings"
)
func main() {
cmdParts := []string{"git", "commit"}
flags := []string{"-m", "Initial commit"}
fullCmd := slices.AppendSeq(cmdParts, flags...)
command := strings.Join(fullCmd, " ")
fmt.Println("Command:", command)
}
We combine base command parts with flags into a single slice, then join them into a space-separated string. This demonstrates real-world usage.
Source
Go experimental slices package documentation
This tutorial covered the slices.AppendSeq function in Go with practical
examples of appending elements to slices in various scenarios.
Author
List all Go tutorials.