Open shikbupt opened 2 years ago
The buildin map looks use unsade.Pointer, not the values of struct:
package main
import "fmt"
type S struct {
I int
}
func main() {
var m = make(map[*S]int)
s1 := &S{I: 1}
m[s1] = 1
fmt.Println(m[s1]) // 1
s2 := &S{I: 1}
fmt.Println(m[s2]) // 0
s1.I = 2
fmt.Println(m[s1]) // 1
}
the struct can be the map key.
package main
import "fmt"
type S struct {
I int
}
func main() {
var m = make(map[S]int)
s1 := S{I: 1}
m[s1] = 1
fmt.Println(m[s1]) // 1
s2 := S{I: 1}
fmt.Println(m[s2]) // 1
s1.I = 2
fmt.Println(m[s1]) // 0
}
the comparable type contains structs whose fields are all comparable types. the hash key of the comparable structs is its pointer, but i think the values of the struct's fields should be the hash key. otherwise, the struct should be saved in the process lifecycle.