3 ms·
I'm lost after reading this. Is the order actually randomized, or does it merely reflect the underlying storage order?
by millstone 13y ago
I'm lost after reading this. Is the order actually randomized, or does it merely reflect the underlying storage order?
- enneff 13y agoIteration is in storage order, but the start point for that iteration is chosen at random.
- deleted 13y ago[deleted]
- jerf 13y agoPut this in a test.go file locally and run it repeatedly with "go run test.go": package main func main() { x := map[string]string{"A": "", "B": "", "C": "", "D": "", "E": ""} for key, _ := range(x) { print(key + "\n") } print("\nNext iter:\n") for key, _ := range(x) { print(key + "\n") } } You'll see the order can change even within a single run. It does not appear to be uniformly random; I had to put 5 keys in because it happened far less often than expected with 3 keys, but you can see randomization often enough with this. Normally, I'd link to the Go Playground, but this doesn't manifest there "correctly", probably due to caching of the results.