3
votes
package main

import "fmt"

func main(){
    sample := map[string]string{
    "key1":"value1",
    "key2":"value2",
    "key3":"value3",
    }
    for i := 0;i<3;i++{
        fmt.Println(sample)
    }
}

The above go code just print a map[string]string three times.

I expect it to a fixed output,but it shows as the following:

map[key3:value3 key2:value2 key1:value1]
map[key1:value1 key3:value3 key2:value2]
map[key2:value2 key1:value1 key3:value3]

It varies!

while in python:

#!/bin/env python
#encoding=utf8

sample = {
    "key1":"value1",
    "key2":"value2",
    "key3":"value3",
}
for i in range(3):
    print sample

OutPut:

{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}`
4
In principle, order is not guaranteed on maps, not even Python's, so you really shouldn't expect a fixed output when printing a map's content. This is probably due to some internal implementation detail. Interesting question, though. - Giulio Piancastelli
@GiulioPiancastelli - In case someone comes along after the first release of Python 3.6 (like me now): In python 3.6+ keyword arguments and dicts respect insert order (for a start in the reference C implementation) - so one can keep the cake and eat it too ;-) The fixed order in python implementations before was only semi-fixed (in python v2 the keys with hash collisions were placed differently depending on insert order) ... - Dilettant

4 Answers

15
votes

You cannot rely on the order in which you will get the keys. The language spec says "A map is an unordered group of elements", and later "The iteration order over maps is not specified and is not guaranteed to be the same from one iteration to the next."

6
votes

Yes, it varies and even intentionally (iteration of a non modified map has been stable before). The intent is to catch as early as possible the situation when someone wrongly assumes a stable iteration guarantee. Additionally, with the added freedom for map implementation there comes more possible future optimizations of that part of the run time library.

3
votes

Python does not guarantee the order of iteration, but it does guarantee that the order will remain stable so long as you do not modify the dictionary between calls:

If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are
called with no intervening modifications to the dictionary, the lists will 
directly correspond.

Go does not guarantee either. It looks from your example as though the order in Go may be stable and only the starting point varies, but as nothing is guaranteed don't depend on it.

0
votes

As I understand that you should not depend on the order of the iterator for the map, since it has no logical ordering.

That aside, I believe that Go's implementation for map iteration is intentionally random (http://nathanleclaire.com/blog/2014/04/27/a-surprising-feature-of-golang-that-colored-me-impressed/ and http://www.confreaks.com/videos/3419-gophercon2014-opening-day-keynote) that is to discourage people from relying on it in their code.

I hope that helps.