liveBook · Manning

Array

As explained in Go in Action, Second Edition

An array is a fixed-length, indexed sequence of values of one element type that is itself a value in Go.

Use a Go array when the number of elements is known and fixed. An array holds one element type in contiguous storage; its zero-based indices provide direct access. Both length and element type belong to its type, so [5]int and [4]int are different types. section 4.1 section 4.1.1

Declare an array with var to get zero-valued elements, or initialize it with a literal: values := [5]int{10, 20, 30} leaves the last two elements at zero. A literal can name particular indices. The [...] form lets the compiler determine the length, but an explicit length is safer when an unintended size would matter. section 4.1.1

Table 4.1 Different types of array declarations

Format

Description

Example

Result

var A [SIZE]TYPE

Array variable declaration

var intArray[5]int

[0 0 0 0 0]

A := [SIZE]TYPE{VALUES}

Array literal with optional starting values

intArray := [5]int{10,20,30}

[10 20 30 0 0]

A := [SIZE]TYPE{INDEX: VALUE}

Array literal with sparse values

intArray := [5]int{1: 20, 3: 40}

[0 20 0 40 0]

A := […]TYPE{VALUES}

Array literal with automatic size

intArray := […]int{10,20,30}

[10 20 30]

Compare ordinary, partially initialized, indexed, and automatically sized declarations. section 4.1.1

For routine traversal, range supplies indices and, optionally, copies of the elements. Changing a range value does not change the array: use the index to assign to an element instead. An indexed for loop also works when traversal must run backward or skip elements. section 4.1.4

Copy a fixed set of values

Assigning one [5]string array to another copies all five elements into a separate array. The same rule applies to nested arrays: [2][2]int is an array of arrays, and grid[1][0] accesses an inner value. If the elements are pointers, assignment copies the pointers, so the two arrays can still refer to the same objects. section 4.1.5 section 4.1.6

An array passed to a function is copied in full. For a large fixed-size image array, passing a pointer avoids that copy, but changes through the pointer affect the original. Arrays cannot grow or shrink; when the number of items varies, use a slice instead. A slice expression can also make a slice backed by an existing array. section 4.1.7 section 4.1.8 section 4.2.6

How Go in Action, Second Edition covers it

How deep it goes
Works through declarations, indexing, iteration, assignment, nested arrays, and function-copy costs; stops short of array-backed storage internals.
Examples
Uses integer and string literals, nested arrays, range-loop mutations, and a large image array passed to a function.
What it assumes
Introduces array declarations from Go variables and literals, then relies on for loops and pointers for iteration and copying decisions.
What you can do after
Build it from scratch with typed array declarations and indexed loops, then choose pointers or slices when copying or growth matters.
How it connects
Connects arrays to slices, range loops, pointers, and pass-by-value function arguments.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.