N-dimensional arrays

March 1, 2023

There’s one sentence at the end of the paragraph yesterday about arrays, which I saved for today, as I think it deserves special attention:

Array types

Array types are always one-dimensional but may be composed to form multi-dimensional types.

[3][5]int
[2][2][2]float64  // same as [2]([2]([2]float64))

The two examples provided do not represent a 2- and 3-dimensional array, therefore. Rather, they represent an array of arrays, and an array of arrays of arrays, respectively.

This might seem like a distinction without a difference, and if we were limited strictly to fixed-length arrays, it might be. But as we’ll see when we discuss the slice type next, we’ll see that this distinction actually makes a difference.

For now, though, just remember that there are no 2-, 3-, or higher-dimensional arrays in Go. Only one-dimensional arrays, which may in turn be of other 1-dimensional arrays, in effect, then, building up N-dimensionality.

Quotes from The Go Programming Language Specification, Version of January 19, 2023


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .