Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Operators

Index and slice expressions: [] and [:]

Reference for Go index and slice expressions: a[i], a[lo:hi], full slice expressions a[lo:hi:max], bounds rules, strings, arrays, maps and panics.

Square brackets do two jobs. An index expression a[i] selects one element of a string, array, slice or map (and instantiates generic functions and types). A slice expression a[lo:hi] produces a new string or slice that covers part of the original, without copying the data.

Slicing is cheap: the result shares memory with its operand. That is powerful and the source of most slice surprises, so it is worth knowing exactly where the bounds and the capacity come from.

Go
first := items[0]
middle := items[2:5]  // elements 2, 3, 4

Try it

Go Indexing and slicing

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 Shared memory and the full slice expression

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
a[i]            // index
a[lo:hi]        // simple slice expression
a[lo:]  a[:hi]  a[:]
a[lo:hi:max]    // full slice expression (not for strings)
Name Description
a A string, array (must be addressable to slice), pointer to array, slice, or map (index only).
i Index: a non-negative integer less than len(a). For maps, a value of the key type.
lo, hi Slice bounds, with 0 <= lo <= hi <= cap(a) (for strings and arrays, len(a)). Defaults: lo is 0, hi is len(a).
max Limits the result's capacity to max - lo; requires 0 <= lo <= hi <= max <= cap(a).

Result

  • For a slice or array, the result is a slice of length hi - lo and capacity cap(a) - lo (or max - lo).
  • For a string, the result is a string of length hi - lo; no capacity.

Errors and panics

  • An index outside the range panics with "index out of range [i] with length n". Constant indexes out of range of an array or constant string are compile errors.
  • A bad slice bound panics with "slice bounds out of range".
  • A nil map can be read (zero values) but writing panics. Slicing a nil slice with [0:0] is fine and yields nil.

Usage notes

  • Indexing a string gives a byte, not a rune or a one-character string. Use range or []rune(s) for characters.
  • Slicing a string can cut a multi-byte UTF-8 character in half. Slice at boundaries found with utf8 or strings functions.
  • The slice shares storage with its source. Writes through one view show up in the other, and a small slice of a huge array keeps the whole array alive. Copy with slices.Clone or copy to detach.
  • hi may exceed len(a) up to cap(a) when slicing a slice, which re-exposes elements beyond the length.
  • Use s[:0] to truncate while reusing the backing array, and s[len(s):] for an empty tail.
  • In generics, F[int] is an instantiation, which looks like indexing. Context decides which one it is.
  • Map index expressions are not addressable: you cannot write &m[k] or assign to m[k].field.
  • Evaluation of out-of-range indexes is checked at run time on every access; the compiler removes checks it can prove unnecessary.