Search⌘ K
AI Features

Multidimensionality

Explore how Go manages multidimensional arrays and slices, including their rectangular and jagged structures. Understand how to initialize and work with nested loops for arrays and dynamic inner slices, enabling efficient data manipulation in Go programs.

Multidimensional arrays

Arrays are always 1-dimensional, but they may be composed to form multidimensional arrays, like:

[3][5]int
[2][2][2]float64

The inner arrays always have the same length. Go’s multidimensional arrays are rectangular. Here is a code snippet ...

Go (1.6.2)
package main
const (
WIDTH = 1920 // columns of 2D array
HEIGHT = 1080 // rows of 2D array
)
type pixel int // aliasing int as pixel
var screen [WIDTH][HEIGHT]pixel // global 2D array
func main() {
for y := 0; y < HEIGHT; y++ {
for x := 0; x < WIDTH; x++ {
screen[x][y] = 0 // initializing value to 2D array
}
}
}

In the code ...