Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Subscript

The subscript postfix operation indexes into a slice or tuple value, returning the resulting value.

Process

At runtime:

  1. the operand is evaluated;
  2. the index expression is evaluated; and
  3. the element at the index is the subscript's result.

For slices, an index outside the slice's range is a runtime error; the compiler emits runtime logic to detect it, unless it can be proven that the index is within range. A negative index value is out of range.

A compile-time constant is an expression composed of integer literals, parenthesized expressions, and the arithmetic and negation operators, which the compiler evaluates at compile time. Arithmetic within a compile-time constant wraps as it would at runtime; a compile-time constant whose evaluation divides or takes a remainder by zero is a compile-time error.

See the runtime error model on the architecture page.

Assertions

  1. The operand's type is a slice type or a tuple type.
  2. The index expression has an integer type, signed or unsigned.
  3. For tuple operands, the index is a compile-time constant within the tuple's arity, and the subscript has the indexed element's declared type; a constant outside the tuple's arity is a compile-time error.
  4. For slice operands, the subscript has the slice's element type.

Syntax

subscript_operation = left_bracket, expression, right_bracket ;