The following go-code works fine:
package main
import (
"fmt"
)
type Node struct {
north int
east int
south int
west int
}
func main() {
roomsX := 5
roomsY := 5
test := [5][5]Node{}
for x := 0; x < roomsX; x++ {
for y := 0; y < roomsY; y++ {
test[x][y] = Node{}
}
}
fmt.Println(test)
fmt.Println(test[0][0].north)
fmt.Println(test[4][4].north)
}
... but if I use variables instead of numbers it doesn't:
test := [roomsX][roomsY]Node{}
getting the following errors:
test2.go:17:11: invalid array length roomsX, test2.go:17:19: invalid array length roomsY
I think it must be possible by using slice but I didn't find how to do it!
I found how to use a slice instead of an array:
... but now I want to have [test] as global, defining it outside the main function, does anyone know how to do it?