Go Language Multidimensional Arrays
Go language supports multidimensional arrays. The following is a commonly used method for declaring multidimensional arrays:
var variable_name [SIZE1][SIZE2]...[SIZEN] variable_type
The following example declares a three-dimensional integer array:
var threedim [5][10][4]int
Two-dimensional Arrays
A two-dimensional array is the simplest form of a multidimensional array. Essentially, it is composed of one-dimensional arrays. The definition of a two-dimensional array is as follows:
var arrayName [x][y] variable_type
variable_type
is the data type in Go, arrayName
is the name of the array, and a two-dimensional array can be considered as a table where x
represents rows and y
represents columns. The following diagram illustrates a two-dimensional array a
with three rows and four columns:
Elements in a two-dimensional array can be accessed using a[i][j]
.
Example
package main
import "fmt"
func main() {
// Step 1: Create an array
values := [][]int{}
// Step 2: Add two one-dimensional arrays to the empty two-dimensional array using append()
row1 := []int{1, 2, 3}
row2 := []int{4, 5, 6}
values = append(values, row1)
values = append(values, row2)
// Step 3: Display the two rows
fmt.Println("Row 1")
fmt.Println(values[0])
fmt.Println("Row 2")
fmt.Println(values[1])
// Step 4: Access the first element
fmt.Println("First element is:")
fmt.Println(values[0][0])
}
The output of the above example is:
Row 1
[1 2 3]
Row 2
[4 5 6]
First element is:
1
Initializing Two-dimensional Arrays
Multidimensional arrays can be initialized using curly braces. The following example initializes a 3-row, 4-column two-dimensional array:
a := [3][4]int{
{0, 1, 2, 3} , /* First row index is 0 */
{4, 5, 6, 7} , /* Second row index is 1 */
{8, 9, 10, 11}} /* Third row index is 2 */
Note: In the above code, the second-to-last }
must have a comma because the last }
cannot be on a separate line. Alternatively, it can be written like this:
a := [3][4]int{
{0, 1, 2, 3} , /* First row index is 0 */
{4, 5, 6, 7} , /* Second row index is 1 */
{8, 9, 10, 11}} /* Third row index is 2 */
The following example initializes a 2-row, 2-column two-dimensional array:
Example
package main
import "fmt"
func main() {
// Create a two-dimensional array
sites := [2][2]string{}
// Add elements to the two-dimensional array
sites[0][0] = "Google"
sites[0][1] = "tutorialpro"
sites[1][0] = "Taobao"
sites[1][1] = "Weibo"
// Display the result
fmt.Println(sites)
}
The output of the above example is:
[[Google tutorialpro] [Taobao Weibo]]
Accessing Two-dimensional Arrays
Two-dimensional arrays are accessed by specifying coordinates. For example, the row index and column index, such as:
val := a[2][3]
or
var value int = a[2][3]
The above example accesses the fourth element of the third row in the two-dimensional array val
.
Two-dimensional arrays can be output using nested loops:
Example
package main
import "fmt"
func main() {
/* Array - 5 rows 2 columns */
var a = [5][2]int{ {0,0}, {1,2}, {2,4}, {3,6},{4,8}}
var i, j int
/* Output array elements */
for i = 0; i < 5; i++ {
for j = 0; j < 2; j++ {
fmt.Printf("a[%d][%d] = %d\n", i, j, a[i][j] )
}
}
}
The output of the above example is:
a[0][0] = 0
a[0][1] = 0
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 4
a[3][0] = 3
a[3][1] = 6
a[4][0] = 4
a[4][1] = 8
a[0][1] = 0 a[1][0] = 1 a[1][1] = 2 a[2][0] = 2 a[2][1] = 4 a[3][0] = 3 a[3][1] = 6 a[4][0] = 4 a[4][1] = 8
The following example creates a multidimensional array with different numbers of elements in each dimension:
Example
package main
import "fmt"
func main() {
// Create an empty two-dimensional array
animals := [][]string{}
// Create three one-dimensional arrays with different lengths
row1 := []string{"fish", "shark", "eel"}
row2 := []string{"bird"}
row3 := []string{"lizard", "salamander"}
// Use the append() function to add one-dimensional arrays to the two-dimensional array
animals = append(animals, row1)
animals = append(animals, row2)
animals = append(animals, row3)
// Loop and print
for i := range animals {
fmt.Printf("Row: %v\n", i)
fmt.Println(animals[i])
}
}
The output of the above example is:
Row: 0
[fish shark eel]
Row: 1
[bird]
Row: 2
[lizard salamander]