Go: what determines the iteration order for map keys?

后端 未结 4 581
[愿得一人]
[愿得一人] 2021-01-17 15:27

The Go Programming Language Specification says:

3. The iteration order over maps is not specified. [...]

That\'s to be expected

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 16:08

    Note that it is not that odd for order to be stable regardless of insertion order if there is a total order over the keys (as there frequently may be if they are of a homogenous type); if nothing else, it can allow efficient searching over keys which generate the same hash.

    This may well also reflect a different underlying implementation - in particular, this is something people might want for strings, but for integers you could use a sparse array instead.

提交回复
热议问题