golang tutorial - Golang Multidimensional Array | Go multidimensional arrays - golang - go programming language - google go - go language - go program - google language
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Two Dimensional declaration
- Go programming language supports multidimensional arrays. Here is the general form of a multidimensional array declaration
var variable_name [SIZE1][SIZE2]...[SIZEN] variable_type
click below button to copy the code. By - golang tutorial - team
- For example, the following declaration creates a three dimensional 4,3,2 integer array:
var wikitechy_array [4][3][2]int
click below button to copy the code. By - golang tutorial - team
Arrays Two-Dimensional structure
- The simplest form of the multidimensional array is the two-dimensional array.
- To declare a two-dimensional integer array of size x,y. Below is the syntax,
var arrayName [ x ][ y ] variable_type
click below button to copy the code. By - golang tutorial - team
- Where variable_type can be any valid Go data type
- arrayName will be a valid Go identifier.
- A two-dimensional array can be think as a table which will have x number of rows and y number of columns.
- A 2-dimensional array a, which contains three rows and four columns which is shown below
- Every element in array a is identified by an element name of the form a[ i ][ j ]
- where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
Initializing Two-Dimensional Arrays
- Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns.
a = [3][4]int{
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
}
click below button to copy the code. By - golang tutorial - team
- Another way of initializing Multidimensional ( Two dimensional ) arrays
var a = [5][2]int{ {0,0}, {8,2}, {2,4}, {3,9},{4,7}}
click below button to copy the code. By - golang tutorial - team
Accessing Two-Dimensional Array Elements
- An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array.
int val = a[2][3]
click below button to copy the code. By - golang tutorial - team
- The above statement will take 4th element from the 3rd row of the array. You can verify it in the above diagram.
- Let us check below program where we have used nested loop to handle a two dimensional array:
package main
import "fmt"
func main() {
/* an array with 5 rows and 2 columns*/
var a = [5][2]int{ {1,0}, {8,2}, {2,4}, {3,9},{4,7}}
var i, j int
/* output each array element's value */
for i = 0; i < 5; i++ {
for j = 0; j < 2; j++ {
fmt.Printf("a[%d][%d] = %d\n", i,j, a[i][j] )
}
}
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Output for the above go program
a[0][0]: 1
a[0][1]: 0
a[1][0]: 8
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 9
a[4][0]: 4
a[4][1]: 7