Structs as keys in Go maps

前端 未结 2 1727
自闭症患者
自闭症患者 2021-02-04 00:10

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 s

相关标签:
2条回答
  • 2021-02-04 00:45

    In your example the map key is a pointer to the struct, not the struct itself. Pointers can be compared for equality even when the items they point to can't be compared. This comparison is not based on the contents of the item, but only on its memory address.

    0 讨论(0)
  • 2021-02-04 00:55

    only comparable type can be used as a key (== ,!=). struct (not a pointer) is comparable in case it contains only comparable types.

    0 讨论(0)
提交回复
热议问题