尋找golang's 2D slices和無法理解在最後一個例子中使用的語法:這個「常用成語」究竟是如何運作的?在文檔
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:]
}
}
我發現如果這被添加到文檔和變化筆者有這個正常/易於理解的代碼change request:
// 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:]
我的問題是如何在上述實現同爲「避免數學」方法?一些關於發生的事情的文檔將會很棒。
敬畏的權利。完全合理。'(picture [i],pixels)=(pixels [:XSize],pixels [XSize:])'。只是沒有看到這種方式。 – Jonathan