SyntaxPilot
Courses/Go Programming/Lesson 14

Go Programming · Lesson 14 of 89 · 10 min

Slices: internals and gotchas

slice header (pointer, length, capacity)backing array sharingaliasing bugs after appendgrowth strategyfull slice expression s[low:high:max]memory retention from subslicingpreallocationslices package (Sort, Contains, Index, Insert, Delete, Equal, Clone, Compact)

These are the notes for Go episode 14. In episode 13 two strange things happened: a 999 appeared in a slice we never touched, and a tea appended inside a function vanished. Today we open a slice up and see exactly why. You will learn the slice header (pointer, length, capacity), backing array sharing, aliasing bugs after append, the full slice expression s[low:high:max], how append grows a slice (from the Go runtime source), preallocation, memory retention from small sub-slices, and the slices package: Sort, Contains, Index, Insert, Delete, Equal, Clone, Compact (plus Reverse). Every example runs as-is in the SyntaxPilot compiler (language: Go); outputs are from real runs (Go 1.24).

What a slice really is: the slice header

Picture a train ticket:

A train ticket A slice Meaning
The coach and its seats backing array the ordinary array where the elements actually live
"From seat 2" pointer the memory address of the slice's first element (episode 11)
"3 seats booked" len elements in use
"6 seats to the end of the coach" cap room from the first element to the end of the backing array

Pointer + length + capacity together are the slice header, and the header is all a slice variable holds. The elements live in the backing array.

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    sales := []int{120, 95, 140, 80, 110, 150, 200}
    week := sales[1:4]

    fmt.Println(len(week), cap(week), week)
    fmt.Println(unsafe.Sizeof(week), "bytes")

    fmt.Println(&sales[1] == &week[0])
}
3 6 [95 140 80]
24 bytes
true
  • cap(week) is 6: from index 1 to the end of the 7-element backing array.
  • unsafe.Sizeof reports how many bytes a variable itself takes. A slice header is 24 bytes on a 64-bit computer (three 8-byte values), no matter how many elements. (The unsafe package is for low-level inspection; you will rarely need it.)
  • &sales[1] == &week[0] is true: the same memory. Two tickets, one coach.

This explains the vanished tea. Passing a slice to a function copies the 24-byte header. The elements are shared, but the function's length is its own copy; append changed that copy only.

Sharing bug #1: append writes into the original

package main

import "fmt"

func main() {
    sales := []int{120, 95, 140, 80, 110, 150, 200}
    first := sales[0:2]

    first = append(first, 0)
    fmt.Println("first:", first)
    fmt.Println("sales:", sales)
}
first: [120 95 0]
sales: [120 95 0 80 110 150 200]

first has length 2 but capacity 7. There was room, so append did not make a new array: it wrote the 0 into the next slot of the shared array, which is sales[2]. Two slices that share memory are aliases; a bug caused by that is an aliasing bug.

Sharing bug #2: two orders from one base

package main

import "fmt"

func main() {
    base := make([]string, 0, 5)
    base = append(base, "chai", "samosa")

    asha := append(base, "bun maska")
    ravi := append(base, "vada pav")

    fmt.Println("asha:", asha)
    fmt.Println("ravi:", ravi)
}
asha: [chai samosa vada pav]
ravi: [chai samosa vada pav]

Both appends wrote into the same third slot of base's backing array; Ravi's write came last and overwrote Asha's item.

The fixes: s[low:high:max] and slices.Clone

The root cause is spare capacity inside someone else's array. Remove it:

  • The full slice expression s[low:high:max] sets the capacity: cap = max - low. With no spare room, the next append must copy to a new array.
  • slices.Clone(s) returns a copy with its own backing array.
package main

import "fmt"

func main() {
    sales := []int{120, 95, 140, 80, 110, 150, 200}
    first := sales[0:2:2]
    fmt.Println(len(first), cap(first))

    first = append(first, 0)
    fmt.Println("first:", first)
    fmt.Println("sales:", sales)
}
2 2
first: [120 95 0]
sales: [120 95 140 80 110 150 200]
package main

import (
    "fmt"
    "slices"
)

func main() {
    base := make([]string, 0, 5)
    base = append(base, "chai", "samosa")

    asha := append(slices.Clone(base), "bun maska")
    ravi := append(slices.Clone(base), "vada pav")

    fmt.Println("asha:", asha)
    fmt.Println("ravi:", ravi)
}
asha: [chai samosa bun maska]
ravi: [chai samosa vada pav]

The slices package has been in Go's standard library (the packages that ship with Go) since Go 1.21.

Indices must satisfy low ≤ high ≤ max ≤ cap(s); otherwise the compiler complains when they are constants:

./main.go:7:16: invalid slice indices: 2 < 3

(from s[1:3:2], where max 2 is less than high 3).

How append grows a slice

package main

import "fmt"

func main() {
    var s []int
    last := cap(s)
    for i := 0; i < 2000; i++ {
        s = append(s, i)
        if cap(s) != last {
            fmt.Print(cap(s), " ")
            last = cap(s)
        }
    }
    fmt.Println()
}
1 2 4 8 16 32 64 128 256 512 848 1280 1792 2560 

The rule, from the Go 1.24 runtime source (src/runtime/slice.go, function nextslicecap):

  1. If the needed length is more than double the old capacity, use the needed length.
  2. If the old capacity is below 256, double it.
  3. Otherwise repeatedly add (cap + 3*256) / 4, that is a quarter of the capacity plus 192, until it is big enough. This eases from about 2x for mid-sized slices toward 1.25x for huge ones, so big slices waste less memory.
  4. Finally the byte size is rounded up to a size class: one of the fixed block sizes Go's memory allocator hands out. 512 → 832 by the formula → 848 after rounding.

This is a runtime detail and can change between Go versions: use it to understand costs, never as something your code depends on.

Preallocation

Every capacity jump allocates a new array and copies every element. Count the jumps for a million appends:

package main

import "fmt"

func count(s []int, n int) int {
    moves := 0
    for i := 0; i < n; i++ {
        if len(s) == cap(s) {
            moves++
        }
        s = append(s, i)
    }
    return moves
}
func main() {
    n := 1_000_000
    fmt.Println("no make:  ", count(nil, n), "new arrays")
    fmt.Println("with make:", count(make([]int, 0, n), n), "new arrays")
}
no make:   38 new arrays
with make: 0 new arrays

1_000_000: underscores in number literals are just for readability. When you know the final size (or a good guess), preallocate with make([]T, 0, n).

Memory retention: keeping the whole newspaper

If you keep a bookmark in a 40-page newspaper, you keep the whole newspaper. A small slice of a big array does the same: while the small slice is reachable, the whole backing array stays in memory, and the garbage collector (the part of the Go runtime that frees memory nothing uses any more) can't free it.

package main

import (
    "fmt"
    "slices"
)

func main() {
    paper := make([]byte, 10_000_000)
    headline := paper[:20]
    fmt.Println(len(headline), cap(headline))

    kept := slices.Clone(paper[:20])
    fmt.Println(len(kept), cap(kept))
}
20 10000000
20 24

headline pins all 10 MB. The clone has its own 24-byte array (20 rounded up to a size class). Keep small pieces of big data as copies.

The slices package

package main

import (
    "fmt"
    "slices"
)

func main() {
    tokens := []int{42, 7, 19, 7, 3}
    slices.Sort(tokens)
    fmt.Println("sorted:  ", tokens)
    fmt.Println("has 19?  ", slices.Contains(tokens, 19))
    fmt.Println("where 19?", slices.Index(tokens, 19))
    fmt.Println("where 50?", slices.Index(tokens, 50))
    fmt.Println("equal?   ", slices.Equal(tokens, []int{3, 7, 7, 19, 42}))
}
sorted:   [3 7 7 19 42]
has 19?   true
where 19? 3
where 50? -1
equal?    true
package main

import (
    "fmt"
    "slices"
)

func main() {
    queue := []string{"Asha", "Ravi", "Meena"}
    queue = slices.Insert(queue, 1, "Grandpa")
    fmt.Println(queue)

    queue = slices.Delete(queue, 2, 3)
    fmt.Println(queue)

    ids := []int{3, 3, 7, 7, 7, 19, 3}
    ids = slices.Compact(ids)
    fmt.Println(ids)
}
[Asha Grandpa Ravi Meena]
[Asha Grandpa Meena]
[3 7 19 3]
Function What it does Returns
slices.Sort(s) sorts in place, ascending nothing
slices.Contains(s, v) is v in s? bool
slices.Index(s, v) first index of v, or -1 int
slices.Equal(a, b) same length and same elements in order? bool
slices.Insert(s, i, v...) inserts values at index i the new slice: assign it
slices.Delete(s, i, j) removes s[i:j] (half-open) the new slice: assign it
slices.Clone(s) a copy with its own backing array the copy
slices.Compact(s) merges runs of equal neighbours into one the new slice: assign it
slices.Reverse(s) reverses in place nothing
  • == doesn't work on slices (slice can only be compared to nil); use slices.Equal.
  • Compact only merges neighbours: the final 3 survived. Sort first to remove all duplicates.
  • Contains and Index walk the slice from the start: for many lookups by value, a map (episode 15) is faster.

Mini project: a leaderboard of unique tokens

package main

import (
    "fmt"
    "slices"
)

func main() {
    tokens := []int{42, 7, 19, 7, 3, 42, 88, 19}
    unique := slices.Clone(tokens)
    slices.Sort(unique)
    unique = slices.Compact(unique)
    fmt.Println("all:   ", tokens)
    fmt.Println("unique:", unique)

    top := slices.Clone(unique[len(unique)-3:])
    slices.Reverse(top)
    fmt.Println("top 3: ", top)
}
all:    [42 7 19 7 3 42 88 19]
unique: [3 7 19 42 88]
top 3:  [88 42 19]
  • Clone first so the original order is kept.
  • Sort + Compact = remove duplicates.
  • Cloning the last three before Reverse keeps unique untouched (otherwise Reverse would flip shared memory).

The next problem: Index searches one element at a time. To find the price of "masala chai" on a 200-item menu we want to jump straight to it by name: maps.

Common mistakes (what really happens)

You wrote What happens Fix
first := sales[0:2]; first = append(first, 0) no error: sales[2] becomes 0 sales[0:2:2] or slices.Clone
two appends to one base slice with spare capacity no error: both see the last item slices.Clone(base) first
keeping big[:20] of a huge slice no error: the whole array stays in memory slices.Clone(big[:20])
slices.Delete(q, 1, 2) without assigning no error: q is [Asha Meena ""], length 3 q = slices.Delete(q, 1, 2)
s[1:3:2] invalid slice indices: 2 < 3 keep low ≤ high ≤ max ≤ cap
a == b on slices invalid operation: a == b (slice can only be compared to nil) slices.Equal(a, b)
slices.Compact on unsorted data duplicates that aren't neighbours survive slices.Sort first

Practice

  1. Predict, then run: var arr [5]int; a := arr[1:2]; a = append(a, 7). What is arr?
  2. Count how many new arrays 10,000 appends to a nil slice need.
  3. Remove duplicate names with Sort + Compact.
  4. Write your own insert(s, i, v) using append and copy.
Solutions
  1. a has length 1 and capacity 4, so the 7 lands in arr[2]:
package main

import "fmt"

func main() {
    var arr [5]int
    a := arr[1:2]
    fmt.Println(len(a), cap(a))
    a = append(a, 7)
    fmt.Println(arr)
}
1 4
[0 0 7 0 0]

2.

package main

import "fmt"

func main() {
    var s []int
    moves := 0
    for i := 0; i < 10_000; i++ {
        if len(s) == cap(s) {
            moves++
        }
        s = append(s, i)
    }
    fmt.Println(moves, "new arrays, final cap", cap(s))
}
19 new arrays, final cap 12288

3.

package main

import (
    "fmt"
    "slices"
)

func main() {
    names := []string{"Ravi", "Asha", "Meena", "Asha", "Ravi"}
    slices.Sort(names)
    names = slices.Compact(names)
    fmt.Println(names)
}
[Asha Meena Ravi]
  1. Grow by one, shift the tail right with copy (copy handles overlapping slices correctly), then set the slot:
package main

import "fmt"

func insert(s []int, i int, v int) []int {
    s = append(s, 0)
    copy(s[i+1:], s[i:])
    s[i] = v
    return s
}

func main() {
    s := []int{10, 20, 40}
    s = insert(s, 2, 30)
    fmt.Println(s)
    s = insert(s, 0, 5)
    fmt.Println(s)
}
[10 20 30 40]
[5 10 20 30 40]

Quick quiz

What does this print?

func main() {
    a := []int{1, 2, 3, 4}
    b := a[:2]
    b = append(b, 9)
    fmt.Println(a)
}
  • A. [1 2 3 4]
  • B. [1 2 9 4]
  • C. [1 2 9]
  • D. [1 2 3 4 9]
Answer

B. [1 2 9 4]. b has length 2 but capacity 4, so append writes 9 into the shared array at a[2].

Interview questions

  • What are the three fields of a slice header? How big is a slice variable?
  • Why can appending to one slice change another?
  • What does s[low:high:max] do, and when would you use it?
  • How does append choose the new capacity? Should your code depend on it?
  • Why does preallocation make code faster?
  • How can a tiny slice keep a huge amount of memory alive? How do you fix it?
  • How do you remove duplicates from a slice?
  • Why do slices.Insert, Delete and Compact return a slice?

Glossary

Term Meaning Example
Slice header the three values a slice variable holds: pointer, len, cap 24 bytes on 64-bit
Pointer (in a header) address of the slice's first element &week[0]
Length elements in use len(s)
Capacity room from the first element to the end of the backing array cap(s)
Backing array the array the elements live in shared by sales and week
Sharing two slices using the same backing array week := sales[1:4]
Alias another name for the same memory first and sales
Aliasing bug a bug where changing one alias changes another sharing bugs #1 and #2
Full slice expression s[low:high:max], sets cap = max - low sales[0:2:2]
Reallocation append making a bigger array and copying into it the capacity jumps
Growth strategy how append picks the new capacity 2x under 256, then easing to 1.25x
Size class a fixed block size the memory allocator uses 832 → 848
Preallocation reserving capacity in advance make([]int, 0, n)
Memory retention a small slice keeping a big array alive paper[:20]
Garbage collector runtime part that frees memory nothing uses frees paper once nothing refers to it
unsafe.Sizeof bytes a variable itself occupies 24 for a slice
Standard library packages that ship with Go fmt, slices
slices.Sort sorts in place [3 7 7 19 42]
slices.Contains membership test true
slices.Index first position of a value, or -1 3
slices.Insert inserts at an index, returns the new slice Grandpa at 1
slices.Delete removes s[i:j], returns the new slice removes Ravi
slices.Equal element-by-element comparison true
slices.Clone copy with its own backing array safe orders
slices.Compact merges equal neighbours [3 7 19 3]
slices.Reverse reverses in place [88 42 19]
Number literal underscore digit separator for readability 1_000_000

Summary

  • A slice is a 24-byte header (pointer, len, cap) over a backing array. Slicing and passing share that array.
  • append into spare capacity writes into memory other slices can see. Stop it with s[low:high:max] or slices.Clone.
  • Growth: double under 256, then ease toward 1.25x, rounded to size classes (runtime detail).
  • Preallocate with make([]T, 0, n); clone small pieces of big slices.
  • The slices package: Sort, Contains, Index, Insert, Delete, Equal, Clone, Compact, Reverse. Assign the results of the ones that return a slice.

Next episode: maps: look values up by key, comma-ok, delete, sets, clear and the maps package.