Essential Go Arrays  Suggest an edit

Multidimensional array

Multidimensional arrays are basically arrays containing other arrays as elements. It is represented like [sizeDim1][sizeDim2]..[sizeLastDim]type, replacing sizeDim by numbers corresponding to the length of the dimension, and type by the type of data in the multidimensional array.

For example, [2][3]int represents an array composed of 2 sub arrays of 3 int typed elements. This could be used to represent a matrix of 2 lines and 3 columns.

It’s possible to create arrays with a lot of dimensions. For example var values := [2017][12][31][24][60]int could store a number for each minute since Year 0.

To access the array in the example above (for example, searching for the value of “2016-01-31 at 19:42”), you would access values[2016][0][30][19][42] (because array indexes start at 0 and not at 1 like days and months)

Some examples follow:

// Defining a 2d Array to represent a matrix like
// 1 2 3     So with 2 lines and 3 columns;
// 4 5 6
multiDimArray := [2] /*lines*/ [3] /*columns*/ int{[3]int{1, 2, 3}, [3]int{4, 5, 6}}

// That can be simplified like this:
var simplified = [2][3]int{{1, 2, 3}, {4, 5, 6}}

// What does it looks like ?
fmt.Println(multiDimArray)
// > [[1 2 3] [4 5 6]]

fmt.Println(multiDimArray[0])
// > [1 2 3]    (first line of the array)

fmt.Println(multiDimArray[0][1])
// > 2          (cell of line 0 (the first one), column 1 (the 2nd one))
[[1 2 3] [4 5 6]]
[1 2 3]
2

// We can also define array with as much dimensions as we need
// here, initialized with all zeros
var multiDimArray = [2][4][3][2]string{}

// the data is:
// > [[[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]]
//   [[[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]]
[2][4][3][2]string{[4][3][2]string{[3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}}, [4][3][2]string{[3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}}}

var multiDimArray = [2][4][3][2]string{}

// We can set some values in the array's cells
multiDimArray[0][0][0][0] = "All zero indexes"   // Setting the first value
multiDimArray[1][3][2][1] = "All indexes to max" // Setting the value at extreme location

// The data looks like:
// > [[[["All zero indexes" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]]
//   [[[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" ""]]]
//    [[["" ""] ["" ""]] [["" ""] ["" ""]] [["" ""] ["" "All indexes to max"]]]]
[2][4][3][2]string{[4][3][2]string{[3][2]string{[2]string{"All zero indexes", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}}, [4][3][2]string{[3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", ""}}, [3][2]string{[2]string{"", ""}, [2]string{"", ""}, [2]string{"", "All indexes to max"}}}}

Arrays/
Create array
Array indexes
Multidimensional array
  ↑ ↓ to navigate     ↵ to select     Esc to close