我使用了一個使用來自句子的單詞作爲其鍵和整數值的地圖。爲什麼count ++(而不是count = count + 1)改變了Golang中地圖的返回方式
func WordCount(s string) map[string]int {
var m map[string]int
m = make(map[string]int)
var substrings[]string
count := 0
substrings = strings.Split(s, " ")
for i := range substrings {
count = count + 1
m[substrings[i]] = count
}
return m
}
func main() {
fmt.Println(WordCount("I am learning GO since some days"))
}
上述代碼ALWAYS顯示地圖以正確的順序,即
map[I:1 am:2 learning:3 GO:4 since:5 some:6 days:7]
但是,如果我改變
count = count + 1
到
count++
釷Ë輸出更改爲:
map[learning:3 GO:4 since:5 some:6 days:7 I:1 am:2]
我知道地圖迭代是隨機的在Golang但爲什麼count = count + 1
總是會導致以有序的方式違背count++
要返回地圖迭代?
難道你有機會在Go Playground上測試它嗎?如果是這樣,操場上的輸出被緩存! – icza
這是來自golang之旅。此代碼中沒有任何內容正在使用巡視的任何現有代碼。 –
我不是在談論代碼的來源,而在於你在哪裏運行/測試它。 – icza