Multidimensionality
This lesson discusses how to handle multidimensional sequences of data in Go.
We'll cover the following...
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 which uses such an array:
Press + to interact
package mainconst (WIDTH = 1920 // columns of 2D arrayHEIGHT = 1080 // rows of 2D array)type pixel int // aliasing int as pixelvar screen [WIDTH][HEIGHT]pixel // global 2D arrayfunc 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 above, we set two constants WIDTH
and HEIGHT
at line 4 and line 5, which determine columns and rows of a 2D array in ...