spec: disallow duplicate keys via variables in keyed constructors #37682
Labels
NeedsDecision
Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone
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
)?go env
OutputWhat did you do?
I have struct like this:
type person struct { first string last string skills []string }
and I have two variables in this type:
` p1 := person{
first: "John",
last: "Stallone",
skills: []string{"Java", "Go"},
}
If I wanted create something like this:
persons := map[string]person{ "Stallone": p1, "Stallone": p2, }
it would be obvious error, but if I try this:
persons := map[string]person{ p1.last: p1, p2.last: p2, }
there is no problem.
And result may be confusing, because second key overwrites previous one. For example for this code:
for _, v := range persons { fmt.Println(v.first) fmt.Println(v.last) for i, val := range v.skills { fmt.Println(i+1, val) } fmt.Println("===") }
I get only one person with skills, when I have map with two values in there.
What did you expect to see?
Consistency would be better (so we should let developer write map with two keys all the time or we should block every try of duplication by raise an exception)
What did you see instead?
Mark Stallone 1 C++ 2 Python 3 Go
The text was updated successfully, but these errors were encountered: