struct
s are stored by value. Accesses to conns["127.0.0.1"]
will give you a copy of the Conn
struct.
If you try to modify the struct like this, the struct at conns["127.0.0.1"]
will remain unchanged until you overwrite the map entry with the newly modified struct:
c := conns["127.0.0.1"]
c.x = y
// `c` now contains different content to `conns["127.0.0.1"]`!
// To ensure conns["127.0.0.1"] is updated, either overwrite or use a point.
conns["127.0.0.1"] = c // overwrite
This is why when you modify the struct, the struct in the map remains unchanged until you overwrite the map entry with the new struct.
Instead, you can store the pointer to the struct. This allows direct modification of the struct.
So if you change the type of conns
from map[string]Conn
to map[string]*Conn
, the first two lines of the above code would update the struct in the map.
More information can be found here: https://www.ardanlabs.com/blog/2017/07/interface-semantics.html