liveBook · Manning

Array

As explained in Learn Go with Pocket-Sized Projects

An array is a Go sequence of a fixed number of values of one type whose length is part of its type.

An array is a fixed-length sequence of values with one element type. The type [n]T contains n values of type T, so [5]string and [4]image.Point are different types. Because length belongs to the type, arrays cannot be resized. section 3.2.2 section 9.3.1

Use arrays when a small, fixed-size collection fits the implementation:

  • Declare an array with a length, such as var a [5]string. section 3.2.2
  • Use [...]image.Point when the compiler should determine the length; four neighboring pixel coordinates form a [4]image.Point array. section 9.3.1
  • Range over arrays with the same index-and-value syntax used for slices. section 3.5

Fixed neighbors in a maze

func neighbours(p image.Point) [4]image.Point { return [...]image.Point{ {p.X, p.Y + 1}, {p.X, p.Y - 1}, {p.X + 1, p.Y}, {p.X - 1, p.Y}, } } The fixed array represents exactly four orthogonal neighbors. Some returned points may lie outside the image; later image access determines how those positions are handled. section 9.3.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines array syntax, type-level length, resizing limits, iteration, and a minor allocation optimization, without deeper runtime internals.
Examples
Demonstrates [5]string declarations and a [4]image.Point neighbor array for maze navigation.
What it assumes
Assumes basic Go syntax, types, functions, and iteration while defining arrays and slices explicitly.
What you can do after
Evaluate/understand when a fixed-size Go collection is appropriate and distinguish it from a slice.
How it connects
Connects arrays to slices, range loops, image.Point, and image bounds handling.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.