To create a Three Dimensional Array in Go, you can use slices of slices of slices.
In this example,
// Creating a 3D Array with Slices
package main
import (
"fmt"
)
func main() {
var threeDArray [][][]int
// Initialize the array
for i := 0; i < 3; i++ {
var inner [][]int
for j := 0; j < 2; j++ {
inner = append(inner, make([]int, 4))
}
threeDArray = append(threeDArray, inner)
}
// Modify an element
threeDArray[1][0][2] = 5
// Print the array
fmt.Printf("%v", threeDArray)
}
[[[0 0 0 0] [0 0 0 0]] [[0 0 5 0] [0 0 0 0]] [[0 0 0 0] [0 0 0 0]]]
In this tutorial, we learned How to create a Three Dimensional Array in Go language with well detailed examples.