How do you create a 3 (or more) dimensional slice in Go?
var xs, ys, zs = 5, 6, 7 // axis sizes
var world = make([][][]int, xs) // x axis
func main() {
for x := 0; x < xs; x++ {
world[x] = make([][]int, ys) // y axis
for y := 0; y < ys; y++ {
world[x][y] = make([]int, zs) // z axis
for z := 0; z < zs; z++ {
world[x][y][z] = (x+1)*100 + (y+1)*10 + (z+1)*1
}
}
}
}
That shows the pattern which makes it easier to make n-dimensional slices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With