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
current golang cannot can take the address of map[x]. like this
m := map[int]int{}
v:= &m[1] // it will compile error
But sometimes this is useful while valueType is struct, like this:
m := map[int]V{}
v, ok := &m[1]
// ok = true if exists.
// ok = false if not exists
if ok {
v.x += 1
v.y += 1
v.z += 1
} else {
m[1] = V{x: 1, y: 1, z: 1}
}
This code is error for golang now, m type must is map[int]*V, the valueType must is a pointer. pointer will increased memory fragmentation and increasing GC
The text was updated successfully, but these errors were encountered:
Hash tables typically grow automatically when the number of elements increases above a certain threshold. When it does, it reallocates a new underlying array and copies all of the old elements into the new one.
If you were able to take the address to an element, and then added an element to the map, causing a growth, what would the address be pointing at?
As you can see it would be very difficult to make pointers to map elements work.
You cannot take the address of a map value because map values are not addressable.
This restriction is in place because the map implementation may have to move values between hash map buckets, so callers must be prevented from taking the address of a map's value.
current golang cannot can take the address of map[x]. like this
But sometimes this is useful while valueType is struct, like this:
This code is error for golang now, m type must is map[int]*V, the valueType must is a pointer. pointer will increased memory fragmentation and increasing GC
The text was updated successfully, but these errors were encountered: