Why does Go's map iteration order vary when printing?

前端 未结 4 901
遥遥无期
遥遥无期 2020-12-18 12:48
package main

import \"fmt\"

func main(){
    sample := map[string]string{
    \"key1\":\"value1\",
    \"key2\":\"value2\",
    \"key3\":\"value3\",
    }
    for          


        
4条回答
  •  不知归路
    2020-12-18 13:26

    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.

提交回复
热议问题