dongzhijing8202 2018-02-19 02:22
浏览 3
已采纳

范围操作员是否保证订购?

Let's say , I am concatenating structs into an array with an append() call. Will the range operator during the read process guarantee the same order as elements were added to the array?

var array_of_structs []*some_struct_t
build_array(&array_of_structs)
for i,value := range array_of_structs {
    ..whatever, but I need `i` to be i=0,1,2... N in an ordered way
}

What do the specs say? I am going to calculate money, so I need to be sure it is ordered.

  • 写回答

2条回答 默认 最新

  • dongzhi5587 2018-02-19 02:29
    关注

    For a slice it is guaranteed by the spec

    For an array, pointer to array, or slice value a, the index iteration values are produced in increasing order, starting at element index 0. If at most one iteration variable is present, the range loop produces iteration values from 0 up to len(a)-1 and does not index into the array or slice itself. For a nil slice, the number of iterations is 0.

    Reference:

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

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图