Go Slices Explained: Length, Capacity, Append and Copy (Examples)
A slice in Go is a small view into an array. It holds three things: a pointer to the first element it can see, a length (how many elements you can index right now) and a capacity (how many elements fit before Go has to allocate a new array). Slices are cheap to pass around because copying a slice copies those three words, not the data.
Most slice bugs come from forgetting that two slices can point at the same array. Every example below was run on Go 1.26, and the output is copied from those runs.
What a slice really is
The runtime’s definition is close to this:
type slice struct {
array unsafe.Pointer
len int
cap int
}
You can make one from an array, with a literal, or with make:
arr := [5]int{1, 2, 3, 4, 5}
s := arr[1:4]
fmt.Println(s, len(s), cap(s)) // [2 3 4] 3 4
t := []int{1, 2, 3} // literal: Go creates the array for you
u := make([]int, 5, 10) // len 5, cap 10, all zeros
s has length 3 but capacity 4, because it starts at index 1 of a 5-element array and can grow to the end of it. Capacity always counts from the slice’s start to the end of the backing array.
How append grows a slice
append adds elements after the current length. If the capacity is big enough, it writes into the existing array and returns a slice with a longer length. If not, it allocates a bigger array, copies everything over and returns a slice pointing at the new one. That’s why you always write s = append(s, ...): the result can be a different slice.
s := make([]int, 0, 5)
s = append(s, 1, 2, 3)
fmt.Println(len(s), cap(s)) // 3 5
s = append(s, 4, 5, 6)
fmt.Println(len(s), cap(s)) // 6 10
The second append didn’t fit, so Go allocated a new array with double the capacity. To see the full growth pattern, I appended 2,000 ints one by one to a nil slice and printed the capacity each time it changed:
[4 8 16 32 64 128 256 512 848 1280 1792 2560]
Small slices double. Past 256 elements the growth factor drops toward 1.25x, and the runtime rounds up to its memory size classes, which is why you get odd numbers like 848. The exact numbers are an implementation detail and change between Go versions, so don’t write code that depends on them. If you know the final size up front, make([]T, 0, n) skips all the reallocations.
Slices share memory
Slicing a slice doesn’t copy anything. Both slices look at the same array, so a write through one shows up in the other:
a := []int{1, 2, 3, 4, 5}
b := a[1:3]
b[0] = 99
fmt.Println(a, b, len(b), cap(b)) // [1 99 3 4 5] [99 3] 2 4
Append is where this gets dangerous. b has length 2 but capacity 4, so appending to it doesn’t allocate. It overwrites a[3]:
b = append(b, 100)
fmt.Println(a, b) // [1 99 3 100 5] [99 3 100]
If you hand a sub-slice to other code and it appends, your original data changes under you. The fix is a full slice expression, a[low:high:max], which caps the capacity so the next append has to allocate:
c := a[1:3:3]
c = append(c, 200)
fmt.Println(a, c) // [1 99 3 100 5] [99 3 200]
a is untouched this time, because c had no spare room and got its own array.
Copying and cloning
When you want independent data, copy it. The built-in copy copies min(len(dst), len(src)) elements and returns that count:
src := []int{1, 2, 3}
dst := make([]int, len(src))
n := copy(dst, src)
dst[0] = 42
fmt.Println(n, src, dst) // 3 [1 2 3] [42 2 3]
Since Go 1.21, the slices package does the common jobs for you. slices.Clone makes a copy, and slices.Sort, slices.Contains and slices.Index cover the loops people used to write by hand:
nums := []int{3, 1, 2}
slices.Sort(nums)
fmt.Println(nums, slices.Contains(nums, 2), slices.Index(nums, 3)) // [1 2 3] true 2
Cloning also fixes a quieter memory problem. A small slice of a big array keeps the whole array alive, because the garbage collector can’t free part of it:
big := make([]byte, 1<<20)
header := big[:16]
kept := slices.Clone(header)
fmt.Println(cap(header), cap(kept)) // 1048576 16
If you only hold on to kept, the 1 MB array can be collected. Holding header keeps all of it in memory.
nil slices vs empty slices
A nil slice and an empty slice both have length 0, and append, len and range treat them the same. The difference shows up when you compare to nil, and in JSON, where encoding/json writes a nil slice as null and an empty slice as []:
var nilS []int
empty := []int{}
fmt.Println(nilS == nil, empty == nil, len(nilS), len(empty)) // true false 0 0
Check for “no elements” with len(s) == 0, not s == nil, and you won’t care which one you have.
Slices are behind a lot of the other posts here. Go maps are the other built-in collection, Go’s string handling works on byte slices under the hood, and the strconv append functions reuse a slice’s spare capacity to build strings without extra allocations. For a real case where one flat slice beat fancier data structures, see rewriting a cloud IP lookup on net/netip.
