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.
snapshots := make(map[string] Snapshot, 1)
, then i think thelen(snapshots)
will be 1, later I initialized the map with one for loop, which used the valuelen(snapshots)
,.... so that mean the initialization process never get run.... then after i used pointer, i get this error:panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x78 pc=0x427bb9d]
– lnshi