Arrays

Understand Go arrays, fixed-size sequences, declaration syntax, value semantics, and working with multi-dimensional arrays.

An array in Go is a fixed-length sequence of elements that all share a single type. You decide the number of elements when you declare the array, and that number stays constant for the lifetime of the array. Arrays are the foundation that slices build on, so getting comfortable with arrays first makes slices far easier to learn.

Array Declaration and Type

An array type is written [n]T, where n is the number of elements and T is the type of each element. The length n is part of the type: [3]int and [5]int are completely different types, so you can never resize an array or assign one array type to another with a different length. Declaring an array without initializing it sets every element to the zero value of its type.

Array Literals

Array literals let you declare and fill an array in one step, with fewer values than the length (the rest zero-fill), with indexed index: value pairs for sparse arrays, or with ... so the compiler counts the length for you. With indexed literals, the length is the highest index + 1, not the number of values you wrote. A partial literal such as [4]string{"alpha"} prints [alpha ], with three trailing empty strings.

Arrays as Values

Go arrays are value types: the variable holds the whole array, not a pointer to it. Assigning an array or passing it to a function copies every element, so changes to the copy never affect the original. To let a function modify the original, or to avoid copying a large array, pass a pointer to the array instead. Two arrays of the same type can be compared with == and !=, provided their element type is comparable.

Multi-Dimensional Arrays

Go builds multi-dimensional arrays by nesting array types: [n][m]T is an array of length n whose elements are arrays of length m. All elements sit in one contiguous block in row-major order. Indexing uses two brackets, grid[i][j], and ranging over a grid yields copies of each row, so modifying elements needs the index form. Copying a multi-dimensional array copies every element, so large grids are usually passed by pointer.

Summary

An array in Go is deliberately simple: it holds a fixed number of elements, its size is part of its type, and copying it means copying every element. These constraints make array behavior predictable and safe, which is why slices are built on top of them rather than replacing them entirely. When you need a collection that stays the same size and you want guaranteed isolation between copies, arrays are the right tool.

Array Declaration and Type in Go

How Go array types work, why the length is part of the type, zero-value behavior, and the practical implications of fixed-size value-type collections.

Array Literals in Go

A complete guide to using array literals for initializing arrays in Go, covering explicit size, indexed initialization, ellipsis inference.

Arrays as Values

Understand how Go arrays behave as value types, including copy-on-assignment, passing by value to functions, and using pointers to avoid unnecessary copying.

Multi-Dimensional Arrays

Learn how Go represents grids and matrices using arrays of arrays, including declaration, initialization, indexing, memory layout, and common pitfalls.