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.

An array in Go is a homogeneous, fixed-size data structure consisting of a contiguous sequence of elements of a single type. The length of an array is an intrinsic part of its type signature, meaning arrays cannot be resized after declaration, and arrays of different lengths (e.g., [3]int and [5]int) are considered distinct, incompatible types by the compiler.

Backing Store for Slices

In idiomatic Go, arrays are rarely manipulated directly. Their primary architectural purpose is to serve as the underlying, fixed-size backing store for slices. Slices act as dynamic, reference-like descriptors that provide a window over these contiguous array memory blocks.

Memory and Value Semantics

Unlike arrays in languages like C or Java, Go arrays are values, not implicit pointers to the first element.
  • Assigning one array to another copies all elements.
  • Passing an array to a function passes a complete copy of the array. The memory allocation for this copy is determined by Go’s escape analysis; it may be allocated on the stack or escape to the heap. Additionally, under Go’s register-based calling convention, small arrays may be passed entirely in CPU registers rather than occupying memory.
  • To avoid copying large arrays, you must explicitly pass a pointer to the array (e.g., *[5]int).

Comparability

Arrays are comparable if their underlying element type is comparable. Two arrays of the same type can be compared using the == and != operators, evaluating to true if all corresponding elements are equal. Because of this property, arrays can be used as map keys—a vital distinction from slices, which are not comparable.
a := [2]int{1, 2}
b := [2]int{1, 2}
c := [2]int{2, 1}

isEqual := a == b    // true
isNotEqual := a == c // false

// Arrays used as map keys
coordinateMap := map[[2]int]string{
    {0, 0}: "Origin",
    {1, 5}: "Point A",
}

Declaration and Initialization

When declared without explicit initialization, an array’s elements are automatically initialized to the zero value of their underlying type.
// Standard declaration: creates an array of 5 integers.
// Memory is allocated and initialized to zero values: [0 0 0 0 0]
var buffer [5]int 

// Array literal initialization
matrix := [3]float64{1.5, 2.0, 3.14}

// Compiler-inferred length using the ellipsis (...) operator
// The compiler counts the elements to determine the type is [4]string
protocols := [...]string{"http", "https", "tcp", "udp"}

// Sparse initialization using index keys
// Initializes indices 2 and 4; all other indices default to 0
// Result: [0 0 100 0 200]
sparse := [5]int{2: 100, 4: 200}

Element Access and Built-in Functions

Array elements are accessed using zero-based indexing. The Go compiler performs bounds checking; attempting to access an index outside the array’s length results in a compile-time error (if the index is a constant) or a runtime panic.
var sequence [3]int
sequence[0] = 10       // Assignment
value := sequence[0]   // Retrieval

// Built-in functions
size := len(sequence)     // Returns the length of the array (3)
capacity := cap(sequence) // Returns the capacity (always equals length for arrays)

Iteration

Arrays can be traversed using a standard for loop or the range keyword. The range keyword yields both the index and a copy of the value at that index.
collection := [...]int{10, 20, 30}

// Iterating using range
for index, value := range collection {
    // 'value' is a copy of collection[index]
}

// Omitting the index using the blank identifier
for _, value := range collection {
    // process value
}

Multi-dimensional Arrays

Go supports multi-dimensional arrays by composing arrays of arrays. The length of every dimension must be defined at compile time.
// A 2x3 two-dimensional array of integers
var grid [2][3]int

// Initialization of a multi-dimensional array
identity := [2][2]int{
    {1, 0},
    {0, 1},
}
Master Go with Deep Grasping Methodology!Learn More