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
package main
import "fmt"
func main() {
m := map[string]string{
"a": "original",
"b": "original",
}
for k, v := range m {
fmt.Println(k, v)
m["a"] = "new"
}
}
Map iteration order unspecified, so if "a" is produced first it will print:
a original
b original
If "b" is produced first, the spec does not specify whether this must print
b original
a new
Or if
b original
a original
is acceptable. In other words, is iteration required to produce the latest version of an entry, or is a stale version OK?
As far as I know, the gc map implementation has always produced the latest version of an entry. In fact, when implementing #54766 I fixed bugs I introduced that produced stale values under the assumption that the spec required it.
The spec does specify "If a map entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced." In my opinion it would be odd to require deletion be tracked precisely, but not changes to values.
Thus I propose that the map portion of https://go.dev/ref/spec#For_range be amended to add "Iteration always produces the latest value of a map entry."
Proposal Details
Consider the following program (https://go.dev/play/p/wd1Ge2PIyAK):
Map iteration order unspecified, so if "a" is produced first it will print:
If "b" is produced first, the spec does not specify whether this must print
Or if
is acceptable. In other words, is iteration required to produce the latest version of an entry, or is a stale version OK?
As far as I know, the
gc
map implementation has always produced the latest version of an entry. In fact, when implementing #54766 I fixed bugs I introduced that produced stale values under the assumption that the spec required it.The spec does specify "If a map entry that has not yet been reached is removed during iteration, the corresponding iteration value will not be produced." In my opinion it would be odd to require deletion be tracked precisely, but not changes to values.
Thus I propose that the map portion of https://go.dev/ref/spec#For_range be amended to add "Iteration always produces the latest value of a map entry."
cc @griesemer @randall77
The text was updated successfully, but these errors were encountered: