Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maps: Clarify iteration order of Keys & Values and that Insert overwrites existing keys #67537

Closed
magical opened this issue May 21, 2024 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@magical
Copy link
Contributor

magical commented May 21, 2024

These functions were added in 2b0f2f8 with minimal documentation.

// Keys returns an iterator over keys in m.
func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K] {
	...
}

// Values returns an iterator over values in m.
func Values[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[V] {
	...
}

// Insert adds the key-value pairs from seq to m.
func Insert[Map ~map[K]V, K comparable, V any](m Map, seq iter.Seq2[K, V]) {
	...
}

The doc comments should be updated to specify the iteration order of Keys and Values, and the behaviour of Insert when duplicate keys are inserted, per discussion on #61900 and in particular rsc's comment:

Happy to update docs to say that iteration order is undefined (different each time) and Insert does overwrite keys.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/587015 mentions this issue: maps: clarify iteration order and insertion behavior

@nightlyone
Copy link
Contributor

What happens when keys are added/removed or values changed while iterating might also be interesting to document.

Maybe it should even all be documented once at a package document and the function document should refer to it

@cagedmantis cagedmantis added compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 21, 2024
@cagedmantis
Copy link
Contributor

cc @ianlancetaylor

@cagedmantis cagedmantis added this to the Go1.23 milestone May 21, 2024
@ianlancetaylor
Copy link
Member

I agree that we should document iteration order. I'm not yet convinced we need to document the behavior for concurrent inserts and deletes from the map.

aimuz added a commit to aimuz/go that referenced this issue May 22, 2024
@dmitshur dmitshur added Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants