On Wed, Jan 6, 2021 at 11:39 PM Amit Saha <amitsaha...@gmail.com> wrote:

> Is it fair to say then that any user-defined type here is acceptable as a
> key?
>

No. You can create a user-defined type out of *any* type T, by just writing
`type X T`. In particular, you can write `type X func()` and get an
incomparable user-defined type.

I highly recommend sticking to the recommendation from the docs: Use `type
ctxKey struct{}` as a key type and `ctxKey{}` as the key - it's comparable
and the only value equal to `ctxKey{}` is `ctxKey{}`, which gives exactly
the desirable semantics. And as it's zero-sized, it also doesn't occur any
allocation (at least with gc, all zero-sized objects have the same
address). You could also use `[0]int` or similar, but `struct{}` is very
common for use-cases like this.


>
>
> >>
> >> Thanks,
> >> Amit.
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> >> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts+unsubscr...@googlegroups.com.
> >> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CANODV3kUmtJvG9Lfys_HXfedwnm54neA6uub6cY1aiz%2B9w5DUA%40mail.gmail.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfH-KNmBMP%2BR904YnBXep3G%3DRDiG7TZzCGHnFMbkA7cOGQ%40mail.gmail.com.

Reply via email to