Skip to content

spec: clarify that modifying map elements in place while iterating is legal #9926

Closed
@jacobsa

Description

@jacobsa

The spec implies that adding to and removing from maps while iterating is legal by defining the semantics of doing so:

If map entries that have not yet been reached are removed during iteration, the corresponding iteration values will not be produced. If map entries are created during iteration, that entry may be produced during the iteration or may be skipped.

As far as I can tell though, it doesn't explicitly specify that existing keys may be modified but not removed. For example:

var m map[string]int

[...]

for k, v := range m {
  m[k] = v + 1
}

Is the above program guaranteed to be legal? Given the strong guarantees on adding and removing elements it seems natural to assume it would be, but explicit clarification from the spec might be good.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions