dongxia8656 2016-10-16 18:18
浏览 19
已采纳

这种“常见习语”实际上是如何工作的?

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.

  • 写回答

2条回答 默认 最新

  • dpspn60064 2016-10-16 18:27
    关注

    This:

    picture[i], pixels = pixels[:XSize], pixels[XSize:]
    

    Is a tuple assignment. It assigns a value to picture[i] and a value to pixels. The values assigned in order are pixels[:XSize] and pixels[XSize:].

    The assignment proceeds in two phases. First, the operands of index expressions and pointer indirections (including implicit pointer indirections in selectors) on the left and the expressions on the right are all evaluated in the usual order. Second, the assignments are carried out in left-to-right order.

    What happens here is that when the loop starts (i = 0), the first element of picture (the first row) is assigned with a slice value being the first XSize elements in pixels, and the pixels slice is resliced so its first element will be the XSizeth element +1.

    So in the next iteration picture[i] will be the 2nd element in the picture (the 2nd row), and again, the first XSize elements from pixels will be set to it as a slice. But since in the previous iteration we resliced pixels, in each iteration its first XSize elements will be the subsequent rows.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成