WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Multidimensional Arrays in Go


Go language supports multidimensional arrays. The following is a common way to declare multidimensional arrays:

var variable_name [SIZE1][SIZE2 ]...[SIZEN]variable_type

The following example declares a three-dimensional array of integers:

var threedim [5][10 ][4]int

two-dimensional array

Two-dimensional arrays are the simplest multidimensional arrays, and two-dimensional arrays are essentially composed of one-dimensional arrays. The two-dimensional array is defined as follows:

var arrayName [ x ][ y  ]variable_type

variable_type is the data type of Go, arrayName is the array name, 2D array can be considered a table, x is the row, y is the column, and the following figure shows a two-dimensional array a is three rows and four columns:

Initialize two-dimensional array

Multidimensional arrays can be initialized with curly braces. The following example is a two-dimensional array of 3 rows and 4 columns:

a = [3][4 ]int{
 {0, 1, 2, 3} , /* The first line index is 0 */
 {4, 5, 6, 7} , /* The second line index is 1 */
 {8, 9, 10, 11}, /* The third line index is 2 */
}
Note: The } of the penultimate line in the above code must have a comma because the last line of } cannot be a single line, Can be written like this:
a = [3][4 ]int{
 {0, 1, 2, 3} , /* The first line index is 0 */
 {4, 5, 6, 7} , /* The second line index is 1 */
 {8, 9, 10, 11}} /* The third line index is 2 */

Access two-dimensional array

Two-dimensional arrays are accessed by specifying coordinates. Such as row index and column index in the array, for example:

val := a< /span>[2][3]
or 
var value int = a[2][3]