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