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

前端 未结 4 900
遥遥无期
遥遥无期 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:32

    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."

提交回复
热议问题