我正在尝试在Golang中初始化2D幻灯片,切片的每个元素始终具有一行并且包含3列或4列。这是我到目前为止所拥有的
package main
import (
"fmt"
)
type Element struct {
rowNumber int
}
type Block [][]Element
func main() {
totalRows := 100
leftBlockRows := 1
leftBlockColumns := 3
middleBlockRows := 1
middleBlockColumns := 4
rightBlockRows := 1
rightBlockColumns := 3
grid := make([][]Block, totalRows)
for i := 0; i < totalRows; i++ {
leftBlock := make(Block, leftBlockRows, leftBlockColumns)
for j := 1; j < leftBlockRows; j++ {
leftBlock[j] = append(leftBlock[j], Element{rowNumber: i})
}
grid[i] = append(grid[i], leftBlock)
middleBlock := make(Block, middleBlockRows, middleBlockColumns)
for j := 1; j < middleBlockRows; j++ {
middleBlock[j] = append(middleBlock[j], Element{rowNumber: i})
}
grid[i] = append(grid[i], middleBlock)
rightBlock := make(Block, rightBlockRows, rightBlockColumns)
for j := 1; j < rightBlockRows; j++ {
rightBlock[j] = append(rightBlock[j], Element{rowNumber: i})
}
grid[i] = append(grid[i], rightBlock)
}
fmt.Println(grid[0])
}
总是打印出最后一张照片
[[[]] [[]] [[]]]
我希望切片grid
具有Element
的实例。但这似乎是空的。我在这里想念什么?