I have the data structure like this:
type Snapshot struct {
Key string
Users []Users
}
snapshots := make(map[string] Snapshot, 1)
// then did the initialization
snapshots["test"] = Snapshot {
Key: "testVal",
Users: make([]Users, 0),
}
Users
is another struct.
Then when I tried to append some new Users
values in the Users slice like this:
snapshots["test"].Users = append(snapshots["test"].Users, user)
I kept getting this error:
cannot assign to struct field snapshots["test"].Users in map
Also tried the workaround here https://github.com/golang/go/issues/3117 so like this:
tmp := snapshots["test"].Users
tmp = append(tmp, user)
snapshots["test"].Users = tmp
But no luck, still exactly same error.
And also tried to declare the map with pointer, so: snapshots := make(map[string] *Snapshot, 1)
, still no luck.
question from:
https://stackoverflow.com/questions/42605337/cannot-assign-to-struct-field-in-a-map 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…