2015-07-03 41 views
-1

以下代碼產生true。所以我想知道在Golang的map[string]string,有沒有辦法區分空字符串和什麼都沒有?如何區分空字符串和地圖中的任何內容

package main 

import "fmt" 

func main() { 
    m := make(map[string]string) 
    m["abc"] = "" 
    fmt.Println(m["a"] == m["abc"]) //true 
} 
+0

謝謝@TimCooper。對不起,我沒有看到這個問題。 –

回答

3

如果「一無所有」你的意思是該元素是不是在地圖上,你可以使用ok成語:

val, ok := myMap["value"] // ok is true if value was in the map 

可以在Effective Go找到更多信息。

+0

非常感謝! –

相關問題