PANIC =运行时错误:索引超出范围

时间:2014-02-28 02:28:40

标签: arrays go indexoutofboundsexception

我有以下代码,但我无法弄清楚错误原因:PANIC=runtime error: index out of range is happening

type Board struct {
    Tboard  [9]string
    Player1 Player
    Player2 Player
}
func makeBoard() *Board {
    b := &Board{Tboard: [9]string{}}
    for x := 0; x < len(b.Tboard); x++ {
        b.Tboard[x] = E // E = "[ ]"
        fmt.Println(b.Tboard[x])
    }
    fmt.Println(len(b.Tboard)) // => 9
    fmt.Print("Error: ")
    fmt.Println(b)        // => Error: %!v(PANIC=runtime error: index out of range)
    fmt.Println(b.Tboard) // => [[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]]
    return b
}

0 个答案:

没有答案