On Thu, Jan 7, 2021 at 9:33 AM Axel Wagner <axel.wagner...@googlemail.com> wrote: > > On Wed, Jan 6, 2021 at 11:31 PM Amit Saha <amitsaha...@gmail.com> wrote: >> >> In the description of context.WithValue(), we have: >> >> The provided key must be comparable and should not be of type string >> or any other built-in type to avoid collisions between packages using >> context. Users of WithValue should define their own types for keys. To >> avoid allocating when assigning to an interface{}, context keys often >> have concrete type struct{}. >> >> I am wondering if someone can explain what exactly comparable means here? >> >> In other languages and Go >> (https://golang.org/ref/spec#Comparison_operators), comparable usually >> means being able to compare two values for equality/greater/lesser. > > > The linked spec section actually contradicts that: > >> The equality operators == and != apply to operands that are comparable. The >> ordering operators <, <=, >, and >= apply to operands that are ordered. > > > (emphasis from the spec). So, the definition of "comparable" is "can be used > with == and !=" and what exactly that means is listed below that quote. > > Hope that helps >
Thanks for pointing it out. I missed that - we have two things here, ordered and comparison here then. Is it fair to say then that any user-defined type here is acceptable as a 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 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/CANODV3n7WUgrU82JygSJ30LiyiJr_Fmbn0L4mQ5s-7NNZbm3Jg%40mail.gmail.com.