查看golang 的 2D 切片的文档,无法理解上一个示例中使用的语法:
func main() {
XSize := 5
YSize := 5
// Allocate the top-level slice, the same as before.
picture := make([][]uint8, YSize) // One row per unit of y.
// Allocate one large slice to hold all the pixels.
pixels := make([]uint8, XSize*YSize) // Has type []uint8 even though picture is [][]uint8.
// Loop over the rows, slicing each row from the front of the remaining pixe ls slice.
for i := range picture {
picture[i], pixels = pixels[:XSize], pixels[XSize:]
}
}
我找到了将其添加到文档中的更改请求,并且更改作者有这个正常/易于理解的代码:
// Loop over the rows, slicing each row.
for i := range picture {
picture[i] = pixels[i*XSize:(i+1)*XSize]
但是,有以下评论:
美好的。另一个常见的成语是避免数学:
picture[i], pixels = pixels[:XSize], pixels[XSize:]
我的问题是上述如何实现与!“避免数学”方法相同的效果?一些关于正在发生的事情的文档会很棒。