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条)

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊