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

proposal: sync: add Map.LoadOrStoreFunc method #67048

Closed
callthingsoff opened this issue Apr 26, 2024 · 3 comments
Closed

proposal: sync: add Map.LoadOrStoreFunc method #67048

callthingsoff opened this issue Apr 26, 2024 · 3 comments
Labels
Milestone

Comments

@callthingsoff
Copy link
Contributor

Proposal Details

I propose adding Map.LoadOrStoreFunc method:

// LoadOrStoreFunc is like LoadOrStore, but receives a function to provide the value.
func (m *Map) LoadOrStoreFunc(key any, valueFunc func() any) (actual any, loaded bool)

Sometimes I don’t immediately know the value to store, and it’s a heavy operation to get the value, I’d like use LoadOrStoreFunc to compute the value lazily. Maybe it’s worth adding this method.

@gopherbot gopherbot added this to the Proposal milestone Apr 26, 2024
@jfrech
Copy link

jfrech commented Apr 26, 2024

Is valueFunc disallowed from accessing m?

@AlexanderYastrebov
Copy link
Contributor

See also previous discussion #44159

@callthingsoff
Copy link
Contributor Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants