Hacker News new | ask | show | jobs
by barsonme 3951 days ago
> Here's a trick that I expected to work but doesn't. Would be interested to hear the rationale if anyone knows it.

Per the spec[0]:

    The comparison operators == and != must be fully defined for operands 
    of the key type; thus the key type must not be a function, map, or 
    slice. If the key type is an interface type, these comparison 
    operators must be defined for the dynamic key values; failure will 
    cause a run-time panic.
[0] - https://golang.org/ref/spec#Map_types
1 comments

By "rationale" I was thinking more along the lines of "here is the reason we chose to do it this way" not just "here is the way we chose to do it".
https://golang.org/doc/faq#map_keys

"Map lookup requires an equality operator, which slices do not implement. They don't implement equality because equality is not well defined on such types; there are multiple considerations involving shallow vs. deep comparison, pointer vs. value comparison, how to deal with recursive types, and so on. We may revisit this issue—and implementing equality for slices will not invalidate any existing programs—but without a clear idea of what equality of slices should mean, it was simpler to leave it out for now.

In Go 1, unlike prior releases, equality is defined for structs and arrays, so such types can be used as map keys. Slices still do not have a definition of equality, though."

Ah, perfect. Thank you.