Two values A and B are equal (A==B) if A and B have the same type and the same values. In a code where
a:=clientContextKey{} b:=clientContextKey{} a==b is true, because they have the same types, and they have the same values (i.e. the empty value). Note however: a:=&clientContextKey{} b:=&clientContextKey{} It is not guaranteed that a==b, neither is it guaranteed that a!=b. The compiler may choose to allocate the same address for the zero-size variables a and b, or it may not, On Sun, May 28, 2023 at 7:01 AM cpu...@gmail.com <cpui...@gmail.com> wrote: > I've recently stumbled across code like this > https://go.dev/play/p/u3UER2ywRlr: > > type clientContextKey struct{} > > ctx := context.Background() > ctx = context.WithValue(ctx, clientContextKey{}, "foo") > _, ok := ctx.Value(clientContextKey{}).(string) > > Naively, I had expected this to fail since I assumed that > clientContextKey{} would create a new variable instance every time it was > used and hence the keys would not match. > > Why does this use of instantiating the struct type work here? > > Thanks, > Andi > > -- > 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/55783f87-bad8-4ca4-9c4a-d8686ae156abn%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/55783f87-bad8-4ca4-9c4a-d8686ae156abn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CAMV2RqoMjPmdo1Xri%3DFAz%2BkXwub-Xo7DPyaX%3DB9%2BSG7fKmbDxg%40mail.gmail.com.