Given a map that uses a struct as its key, where the values of the struct are pointers to another struct:
type Dog struct {
Name string
}
type Cat struct {
Name string
}
type MapKey struct {
dog *Dog
cat *Cat
}
myMap := make(map[MapKey]int)
How would I use the cmp package to make the below maps equal, where they are considered equal because the MapKey has the same values (reflect.DeepEquals or cmp.Equals)?
keyOne := MapKey{
&Dog{Name: "bob"},
&Cat{Name: "clive"},
}
keyTwo := MapKey{
&Dog{Name: "bob"},
&Cat{Name: "clive"},
}
got := map[MapKey]int{
keyOne: 1,
}
want := map[MapKey]int{
keyTwo: 1,
}
In the cmp documentation, it says I could use cmpopts.SortMaps (https://pkg.go.dev/github.com/google/go-cmp/cmp#Equal), however I don't see how this is relevant to my scenario.
I've tried defining a custom Equals function on the MapKey struct but it never gets called.
Go playground to reproduce this: https://go.dev/play/p/qMxaya3S26M