Golang append to slice: elements, another slice, and []byte rules

Golang append and append golang: golang add to slice, golang append to slice and golang slice append with variadic ..., golang append slice to slice, go append to array semantics, []byte plus string append, and always assign the append return value.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang append to slice: elements, another slice, and []byte rules

The built-in append is how you golang add to slice values: it appends zero or more elements of type E to a slice []E and returns a new slice header (often you assign back to the same variable). Searches like golang append, append golang, golang append to slice, golang slice append, go append, or go append to array all point here—remember Go’s arrays are fixed, so you usually append to a slice backed by an array. For variadic unpacking (...), see variadic functions in Go; for merging with uniqueness rules, see concatenate slices (unique); for removing items, see delete from slice.

Tested with Go 1.24 on Linux.

A slice is a small descriptor (pointer, length, capacity) over a backing array; append may grow capacity and move data when there is no room left.

Slice header and backing array (concept)


golang append: one value or many

append(s, x1, x2, ...) returns []E with the new elements at the end. Always keep the return value:

go
package main

import "fmt"

func main() {
	s := []int{1, 5}
	s = append(s, 2)
	s = append(s, 3, 5, 7)
	fmt.Println(s)
}
Output

You should see [1 5 2 3 5 7].


golang append slice to slice (...)

To append all elements of another slice, spread the second slice with ... (same element type required):

go
package main

import "fmt"

func main() {
	a := []string{"Hello", "world"}
	b := []string{"from", "Go"}
	out := append(a, b...)
	fmt.Println(out)
}
Output

You should see [Hello world from Go]. Overlapping source and destination slices are allowed; append still does the right thing for the documented cases.


go append to array (via slicing)

You cannot change an array’s length, but you can append to a slice view of it:

go
package main

import "fmt"

func main() {
	var arr [2]int
	s := arr[:]
	s = append(s, 9)
	fmt.Println("slice:", s, "array:", arr)
}
Output

You should see slice: [0 0 9] and array: [0 0]. Once append grows past the array’s capacity, it allocates a new backing array, so the fixed-size arr no longer receives further elements—this is why growable buffers are usually []T from the start, not [n]T with append.


[]byte and string: special append rule

For []byte, you may append individual bytes or append the bytes of a string using ...:

go
package main

import "fmt"

func main() {
	out := append([]byte("Hello "), "GoLinuxCloud!"...)
	fmt.Println(string(out))
}
Output

You should see Hello GoLinuxCloud!.


Heterogeneous values with []any

A []any ([]interface{}) slice can hold mixed dynamic types; each append still adds values of concrete types:

go
package main

import (
	"fmt"
	"time"
)

func main() {
	var v []any
	v = append(v, 25, 3.14, "hello", time.Date(2026, 6, 17, 0, 0, 0, 0, time.UTC))
	fmt.Println(len(v), v[0], v[2])
}
Output

You should see 4 25 hello (middle fields vary). For typed APIs, prefer structs or separate slices instead of []any unless you really need a dynamic bag.


Summary

append covers golang append, golang append to slice, and golang slice append patterns: pass new elements, or pass another slice with ... for golang append slice to slice. Treat go append to array as append to a slice view or use a slice from the start. For []byte, you can append string bytes with .... Always assign the result (s = append(s, x)) because the backing store may change. Pair this with the Go spec on append and copy and Slices intro blog.


References


Frequently Asked Questions

1. Why must I write s = append(s, x) in Go?

append may allocate a new backing array; it returns a new slice header pointing at that storage, so you must use the returned slice or later writes might hit the old backing array or appear to disappear.

2. How do I golang append slice to slice?

Unpack the second slice with dots: out := append(a, b...); both operands must be the same element type E for []E.

3. Can I go append to array?

Go arrays have fixed length; you cannot append to an array directly. Slice the array (e.g. arr[:]) to get a []T with that backing array, append, and store the result in a slice variable.

4. Can I append a string to []byte?

Yes; append accepts bytes from a string using the ... form after the string, which appends the UTF-8 bytes of the string to the byte slice.

5. Does append copy overlapping slices safely?

Yes; append and copy are defined to handle overlapping source and destination correctly for their documented behaviors.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …