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

GO should allow temporarily taking address of map[x] #29109

Closed
szmcdull opened this issue Dec 5, 2018 · 1 comment
Closed

GO should allow temporarily taking address of map[x] #29109

szmcdull opened this issue Dec 5, 2018 · 1 comment

Comments

@szmcdull
Copy link

szmcdull commented Dec 5, 2018

What version of Go are you using (go version)?

$ go version
All versions

What did you do?

type User struct {
...
}
Users := map[string]User{}
Users["Alex"].Name = "Alex"

What did you expect to see?

compile without error

What did you see instead?

error: cannot assign to struct field Users["Alex"].Name in map

I understand a map is something like a heap. When it grows the whole map will be reallocated at a new address. So taking an address of it is dangerous. But it may be safe to take its address temporarily, especially when reading/writing one of its elements, but not altering the size of it. So that it is more efficient without copying a big element just to modify a little part of it. And the code will be more readable and expressive.

@randall77
Copy link
Contributor

Duplicate of #3117.

@golang golang locked and limited conversation to collaborators Dec 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants