In go, map iteration order is not specified, and may vary from run to run. Is there any way to set a seed for this, such that two runs of the same go program will have consistent iteration order?
In go, map iteration order is not specified, and may vary from run to run. Is there any way to set a seed for this, such that two runs of the same go program will have consistent iteration order?
Brief:
Use official range + map is impossible, but you can use 3rd party libraries, as mentioned in the comments.
Why:
As you can see from here go/src/runtime/map.go#mapiterinit, the traversal of the map uses go/src/runtime/stubs.go#fastrand to randomly choose the starting position, but the result of fastrand() is changed every time, you can't decide the order each time.
It is recommended to briefly read the important parts of the posted source code, everything is very simple and clear.