4 ms·
Just tried go1.2.1. Hashmap iteration order was not random, it was same every time. Same happens with Go playground: (http://play.golang.org/p/px7C_lCn6d http:/
by vardump 13y ago
Just tried go1.2.1. Hashmap iteration order was not random, it was same every time. Same happens with Go playground: (http://play.golang.org/p/px7C_lCn6d http://play.golang.org/p/px7C_lCn6d).
I got this output by running same locally (go1.2.1.darwin-amd64-osx10.8.pkg):
$ go run test.go
There are 500 views for unix
There are 300 views for python
There are 100 views for go
There are 4000 views for javascript
There are 800 views for testing
There are 500 views for unix
There are 300 views for python
There are 100 views for go
There are 4000 views for javascript
... same order is repeated forever.
- teraflop 13y agoThe ordering is only quasi-random, depending on how keys are distributed across hash buckets. Your particular example will no longer have a consistent iteration order starting in Go 1.3. https://code.google.com/p/go/issues/detail?id=6719 https://code.google.com/p/go/issues/detail?id=6719
- vardump 13y agoSo random order in Go1.3. Sounds good to me. Tried anyways with ten items, the results are interesting: http://play.golang.org/p/dVZvmBOu_g http://play.golang.org/p/dVZvmBOu_g With ten items, it seems to randomly (?) alternate between two iteration orders. Local Go1.2.1 has similar behavior.
- ahuth 13y agoThe Go playground and tour won't generate random numbers: "Note: the environment in which these programs are executed is deterministic, so rand.Intn will always return the same number." Could this cause iteration order to be the same when executed there?
- vardump 13y agoPerhaps, but I'm getting analogous results when running exactly same code locally.