Define map with struct as a key where struct has pointer(s) defined

Hi Folks,
I am struggling to make a map work which has a struct as a key where the struct has a pointer.

type Person struct{
name string
height *float64

height1 := float64(5.3)
height2 := float64(5.3)
p1:= &Person{name:“ab”, height:&height1}
p2:= &Person{name:“ab”, height:&height2}

myMap := map[Person]string{p1:“hello”}

if _, found := myMap[p2]; !found{
// above condition is true while p2 and p1 has exact same data but pointers to heights are different

What are my options to actually make such struct work as keys for map? In C# if you override equality operator on struct/class then things work just fine but I see no such option in Go language.
Any help will be appreciated.


You cannot. You could however create a string representation that you could use as the map key, or not use a pointer.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.