Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The [] token in Go serves multiple distinct roles depending on its context: as an index and slice operator for data structures, as a syntax component for declaring array, slice, and map types, and as the delimiter for generic type parameters and instantiation.

Index Operator

When provided with a single expression inside the brackets, [] acts as the index operator. It retrieves or mutates a specific element at a given index or key.
func indexOperatorExamples() {
    // Arrays, Slices, and Strings
    collection := []int{10, 20, 30}
    element := collection[1]
    collection[1] = 42

    // Maps
    mapCollection := map[string]int{"key": 100}
    value, exists := mapCollection["key"]

    // Blank identifiers to ensure compilable code
    _, _, _, _ = element, collection, value, exists
}
  • Arrays, Slices, and Strings: The index must be an expression of any integer type, or an untyped constant representable by a value of type int. Go utilizes zero-based indexing. Accessing an index outside the bounds 0 <= index < len(collection) triggers a runtime panic. Strings are immutable; their elements (bytes) can be accessed via indexing but cannot be mutated.
  • Pointers to Arrays: Go automatically dereferences pointers to arrays when using the index operator. ptr[i] is evaluated as (*ptr)[i].
  • Maps: The expression inside the brackets must be assignable to the map’s defined key type. If the key is absent, the operator evaluates to the zero value of the map’s value type. It uniquely supports a two-value assignment to verify key existence.

Slice Operator

When the brackets contain one or two colons, [] acts as the slice operator. It constructs a new slice descriptor or string that references a contiguous sub-sequence of the original underlying memory. It cannot be applied to maps.
func sliceOperatorExamples() {
    collection := [5]int{0, 10, 20, 30, 40}

    // Simple Slicing (One Colon)
    simpleSlice := collection[1:4] // Evaluates to []int{10, 20, 30}

    // Full Slicing (Two Colons)
    fullSlice := collection[1:3:4] // Evaluates to []int{10, 20}, capacity 3

    _, _ = simpleSlice, fullSlice
}
  • Simple Slicing ([low:high]): Applies to arrays, pointers to arrays, slices, and strings. low is the inclusive starting index (defaults to 0), and high is the exclusive ending index (defaults to len(collection)). The resulting length is high - low. For arrays and slices, indices must satisfy 0 <= low <= high <= cap(collection). For strings, indices must satisfy 0 <= low <= high <= len(collection).
  • Full Slicing ([low:high:max]): Applies only to arrays, pointers to arrays, and slices. It explicitly controls the capacity of the resulting slice, calculated as max - low. Indices must satisfy 0 <= low <= high <= max <= cap(collection). The low index can be omitted, but high and max must be explicitly declared.

Type Declaration Syntax

The [] token is a core component of Go’s type system syntax, utilized to declare array, slice, and map types.
type DataStructures struct {
    // Slice type declaration (empty brackets)
    DynamicList []float64

    // Array type declaration (requires constant length expression)
    FixedList [10]int

    // Map type declaration (requires key type inside brackets)
    Dictionary map[string]bool
}

Generics (Type Parameters and Instantiation)

Introduced in Go 1.18, the [] token is used to define type parameter lists for generic functions and types, and to instantiate those generic constructs within expressions.
// Type parameter list in a generic type definition
type Box[T any] struct {
    Content T
}

// Type parameter list in a generic function definition
func Identity[T any](value T) T {
    return value
}

func genericExamples() {
    // Type instantiation inside an expression
    intBox := Box[int]{Content: 42}
    
    // Explicit type instantiation in a function call
    result := Identity[int](10)

    _, _ = intBox, result
}
  • Type Parameter Lists: Used in declarations (e.g., [T any]) to specify that a type or function accepts one or more type parameters, defining constraints for those types.
  • Type Instantiation: Used within expressions to provide concrete type arguments to a generic type or function (e.g., Box[int]), resolving the generic construct into a specific, usable type at compile time.
Master Go with Deep Grasping Methodology!Learn More