51

Assuming the following

type User struct {
    name string
}

users := make(map[int]User)

users[5] = User{"Steve"}

Why isn't it possible to access the struct instance now stored in the map?

users[5].name = "Mark"

Can anyone shed some light into how to access the map-stored struct, or the logic behind why it's not possible?

Notes

I know that you can achieve this by making a copy of the struct, changing the copy, and copying back into the map -- but that's a costly copy operation.

I also know this can be done by storing struct pointers in my map, but I don't want to do that either.

2

2 Answers 2

73

The fundamental problem is that you can't take the address of an item within a map. You might think the compiler would re-arrange users[5].name = "Mark" into this

(&users[5]).name = "Mark"

But that doesn't compile, giving this error

cannot take the address of users[5]

This gives the maps the freedom to re-order things at will to use memory efficiently.

The only way to change something explicitly in a map is to assign value to it, i.e.

t := users[5]
t.name = "Mark"
users[5] = t

So I think you either have to live with the copy above or live with storing pointers in your map. Storing pointers have the disadvantage of using more memory and more memory allocations, which may outweigh the copying way above - only you and your application can tell that.

A third alternative is to use a slice - your original syntax works perfectly if you change users := make(map[int]User) to users := make([]User, 10)

12
  • 3
    I still don't understand why I can't assign to a field of the struct in a map while I can still access the field. I can only read but not write the field. Can you please clarify why reading the field is not using the address?Thanks
    – zyfo2
    Feb 23, 2017 at 15:08
  • 2
    @zyfo2: there's no fundamental reason the compiler and runtime couldn't arrange for m[key].field = value to compile into temp := m[key]; temp.field = value; m[key] = temp, or an optimized version thereof—and we'd almost certainly want the optimized version, which really would require some help from the runtime—but the language spec simply does not say that Go compilers must do that, and they don't.
    – torek
    Dec 16, 2019 at 8:52
  • 1
    Meanwhile, it might help to realize that current implementations turn temp := m[key] into the compile-time equivalent of: var temp T; memoryCopy(unsafe.Pointer(&temp), mapLookup(m, unsafe.Pointer(&key)), unsafe.Sizeof(unsafe.Pointer(&temp))). That is, the map lookup operation returns a pointer to the found element, or to a zeroed out element. The compiler copies the bytes from this returned pointer into the temporary variable as quickly as possible, before the returned pointer becomes invalid.
    – torek
    Dec 16, 2019 at 8:55
  • 1
    Sure, the language creators would turn around and do a non-obvious thing of stating that while a "regular" map read, v = m[i], would copy the value out, the "modify field" access, m[i].f = v would either require taking the address of the map's element at i or defined to be a three-step operation—copy the value out, mutate the field, copy the value in. The former would logically require bringing the ability to address map elements to the language's proper, and the latter would imply non-obvious runtime costs. Also, how would you define the semantics of m[i].a, m[i].b = m[i].c, m[i].d?
    – kostix
    Dec 16, 2019 at 12:55
  • 2
    Hence, I think the language designers opted to implement the clearest possible semantics: "maps contain values, operation on map elements load and store values of the corresponding types; map elements are not addressable to permit maps move their storage around'.
    – kostix
    Dec 16, 2019 at 12:58
11
  1. Maps are typically sparsely filled hash tables which are reallocated when they exceed the threshold. Re-allocation would create issues when someone is holding the pointers to the values
  2. If you are keen on not creating the copy of the object, you can store the pointer to the object itself as the value
  3. When we are referring the map, the value returned is returned "returned by value", if i may borrow the terminology used in function parameters, editing the returned structure does not have any impact on the contents of the map
1
  • This post answers all my doubt. Dec 1, 2020 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.