Looking at documentation for golang's 2D slices and cannot understand the syntax used in the last example:
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:]
}
}
I found the change request where this was added to the docs and the change author had this normal / easy to understand code:
// Loop over the rows, slicing each row.
for i := range picture {
picture[i] = pixels[i*XSize:(i+1)*XSize]
However, there is the following comment:
fine. another common idiom is to avoid the math:
picture[i], pixels = pixels[:XSize], pixels[XSize:]
My question is how does the above achieve the same as the !"avoid the math" method? Some docs on what is going on would be great.