I was looking into using structs as keys in golang maps. A field in this struct is supposed to be a map also and this seems to go against the documentation provided here which says that only structs that have fields that can be compared with ==
and !=
can be in the fields of structs that are used as keys in maps. I however went ahead to try the following:
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
}
The output was:
Hello, 世界
obi$56
testing map[obi:0.003] go home
testing2 map[obi:0.003] come home
This seems to go against the documentation as a field in the struct used as a key is a map. What do I seem to be getting wrong?