Golang地图排序 [英] Golang map sort
问题描述
我有这样的代码: https://play.golang.org/p/ubwUH1llxc
此地图将会更新。追加新的变种和删除。
有时打印:
<1>
6两个
三个三
五四
并且可以打印:
5四
6两个
<3>
<1>
为什么要打印?
我如何通过最新打印?我不使用数组作为命令,因为元素,之前需要元素,可以删除earlie。我担心它会删除另一个元素。
当使用范围循环迭代地图时,迭代顺序不是指定并且不能保证从一次迭代到下一次迭代是一样的。
你不能确保在不存储密钥的情况下通过最新的方式进行打印。 b
$ b
阅读此处: doc p>
您可以存储密钥并维护订单。请参阅此
同样,如果你想以相反的顺序打印。以相反的顺序迭代数组。
I have this code: https://play.golang.org/p/ubwUH1llxc This map will be update. Append new var and delete.
Sometime it print:
1 One
6 two
3 Three
5 Four
And can print:
5 Four
6 two
3 Three
1 One
Why it print that? How I can print by newest? I don't use array for order, because element, that before need element, can be delete earlie. And I worry, that it delete another element.
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.
You can't surely print by newest without storing key somewhere else.
Read this: doc
You can store key and maintain order. See this
Again, if you want to print in reverse order. Iterate array in reverse order.
这篇关于Golang地图排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!