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
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.
The text was updated successfully, but these errors were encountered:
What version of Go are you using (
go version
)?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.
The text was updated successfully, but these errors were encountered: