du7535 2018-10-28 23:01
浏览 72
已采纳

使用范围时,GO迭代地图条目的顺序是否总是相同? [重复]

This question already has an answer here:

Will this code always display the same result? Underlying question: will range always iterate a map in the same order?

m := map[string]int {
    "a": 1,
    "b": 2,
    "c": 3,
    "d": 4,
    "e": 5,
    "f": 6,
}
for k, v := range m {
    fmt.Printf("%v = %v", k, v)
}
</div>
  • 写回答

2条回答 默认 最新

  • duanben1909 2018-10-28 23:04
    关注

    No, it is intentionally randomized (to keep programmers from relying on it, since it is not specified in the language spec).

    from the Go Blog

    Iteration order

    When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next. Since the release of Go 1.0, the runtime has randomized map iteration order. Programmers had begun to rely on the stable iteration order of early versions of Go, which varied between implementations, leading to portability bugs.

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备