To iterate over a Two Dimensional Array in Go, you can use nested loops.
In this example,
matrix
.package main
import "fmt"
func main() {
matrix := [][]int{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}}
for i := 0; i < len(matrix); i++ {
for j := 0; j < len(matrix[i]); j++ {
fmt.Print(matrix[i][j], " ")
}
fmt.Println()
}
}
0 1 2 1 2 3 2 3 4
In this example,
matrix
.package main
import "fmt"
func main() {
matrix := [][]string{{"Hello", "World"}, {"Go", "Programming"}}
for i := 0; i < len(matrix); i++ {
for j := 0; j < len(matrix[i]); j++ {
fmt.Print(matrix[i][j], " ")
}
fmt.Println()
}
}
Hello World Go Programming
In this tutorial, we learned How to Iterate over a Two Dimensional Array in Go language with well detailed examples.