Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Built-in Types

Go slices

Reference for Go slices: the pointer-length-capacity header, nil versus empty, slicing expressions, full slice expressions, append, copy and aliasing.

A slice describes a contiguous section of an underlying array. It is a three-word header: a pointer to the first element, a length, and a capacity. Slices are the everyday sequence type in Go. They grow with append, can be sliced again cheaply, and are passed to functions without copying their elements.

Because several slices can share one backing array, writes through one slice can show up in another. Understanding length, capacity and sharing is the key to using them safely.

Go
nums := []int{1, 2, 3}
nums = append(nums, 4)
part := nums[1:3] // [2 3]

Try it

Go Length, capacity and sharing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go nil, empty and copy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
var s []T                 // nil slice
s := []T{a, b, c}         // literal
s := make([]T, len, cap)  // zeroed elements
sub := s[low:high]        // length high-low, capacity cap(s)-low
sub := s[low:high:max]    // capacity max-low
Part Description
len(s) Number of elements accessible by indexing.
cap(s) Elements available from the slice's start to the end of its backing array.
low, high Indexes with 0 <= low <= high <= cap(s); low defaults to 0 and high to len(s).
max Optional third index (full slice expression), high <= max <= cap(s).

Edge cases: s[i] with i >= len(s) panics even when i < cap(s). Reslicing with bounds beyond capacity panics. A string can also be sliced, but the result is a string, not a slice.

Usage notes

  • nil versus empty. A nil slice has length 0 and works with len, range and append. Only s == nil distinguishes it from an empty slice, and encoding/json encodes them differently (null versus []).
  • Slices are not comparable with == (except to nil). Use slices.Equal.
  • Append may or may not reallocate. If capacity allows, append writes into the shared array; otherwise it allocates a new array. Always assign the result back (s = append(s, x)).
  • Aliasing bugs appear when two slices reslice the same array and one appends. Use a full slice expression or slices.Clone to separate them.
  • Large arrays stay alive as long as any small slice points into them. Copy what you need to let the rest be collected.
  • Passing a slice copies only the header, so the callee can change elements but not the caller's length. Return the new slice or pass a pointer to change length.
  • Since Go 1.21 the slices package offers Sort, Index, Insert, Delete, Clone, Compact and more; clear(s) zeroes all elements.
  • Growth is amortized: capacity grows roughly 2x for small slices and more slowly for large ones. Do not rely on exact numbers.