Thank you. Zero values are of course identical. I was only thinking about the pointer case...
On Sunday, May 28, 2023 at 5:10:33 PM UTC+2 burak serdar wrote: > 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 <cpu...@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...@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/4e9150e4-36b0-4976-a58f-804391873fbfn%40googlegroups.com.