You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is an error to specify multiple elements with the same field name or constant key value. For non-constant map keys, see the section on evaluation order.
The crux of the question is whether the key you are using is a constant. https://go.dev/ref/spec#Constants lists the constants, and structs (empty or otherwise) are not constants. Even if they are obviously constant to you and me, they are not "Go constants" according to the spec.
For non-constants, we run each entry in the literal as an insert, in program order. There's no error possible in that scenario, we just get subsequent keys overwriting previous ones. That's why you get {}:2 at the end.
vet could warn about "ast"-identical, side effect free, non-pointer* struct keys in map composite literals. Seems feasible to do it.
It is not clear how often people would write map[struct{}]int though. This is an unclear way to write an optional int compared to struct{int; bool} or *int. For more complex examples does this happen in practice? So I am worried about meeting the frequency requirement for vet (README). Is there another example that happened in practice? Maybe this is just theoretically surprising?
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?Reproducible using https://go.dev/play/p/IV_Nb547iZY
What did you do?
Execute the following code :
What did you expect to see?
An error
duplicate key struct{} in map literal
What did you see instead?
The program executes without issue and prints the following:
The text was updated successfully, but these errors were encountered: