14
我正在使用結構作爲golang地圖中的鍵。這個結構中的一個字段應該也是一個映射,這似乎違背了提供的文檔here,其中說只有具有可以與==
和!=
比較的字段的結構可以位於用作鍵的結構的字段中在地圖上。但我繼續嘗試以下操作:在Go地圖中作爲鍵的結構
package main
import "fmt"
import "strings"
func main() {
fmt.Println("Hello, 世界")
fmt.Println(strings.Join([]string{"obi", "$", "56"}, ""))
z := make(map[string]float64)
z["obi"] = 0.003
x := &test{
name:"testing",
code:z,
}
a := &test{
name:"testing2",
code:z,
}
y := make(map[*test] string)
y[x] = "go home"
y[a] = "come home"
for key, val := range y{
fmt.Println(key.name, key.code, val)
}
}
type test struct{
name string
code map[string]float64
}
產量爲:
Hello, 世界
obi$56
testing map[obi:0.003] go home
testing2 map[obi:0.003] come home
這似乎違背了文檔,如用作鍵的結構字段是地圖。我似乎有什麼錯誤?
問題,會出現這樣的情況,其似乎有一個地圖的關鍵是指針的x型? – Roylee
當然,當你對對象的身份比對內容的平等更感興趣時。例如,你可以在一個web服務器中的map [* http.Request]字符串或其他東西中存儲一些額外的每個請求數據。 – andybalholm
這是一個很好的,謝謝;) – Roylee