> ## 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.

# Go Array

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.

```go theme={"dark"}
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.

```go theme={"dark"}
// 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.

```go theme={"dark"}
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.

```go theme={"dark"}
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.

```go theme={"dark"}
// 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},
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Go Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
