On Thu, Jan 7, 2021 at 9:48 AM Axel Wagner
<[email protected]> wrote:
>
>
>
> On Wed, Jan 6, 2021 at 11:39 PM Amit Saha <[email protected]> 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. If I wanted to have a struct like this
type myReqContext struct {
ID1 string
ID2 string
}
Would the correct key and value be: myReqContext{}, myReqContext{ID1:
"abcd", ID2: "defg"}?
The key isn't zero value any more, or is it? If so, should I have a
dedicated empty struct for the key?
>
>>
>>
>>
>> >>
>> >> 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 [email protected].
>> >> 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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/CANODV3mDhTeORcYP-jzGm2Ckjkp37-mcMX6ich1SWtwDAnAWjQ%40mail.gmail.com.