I am having problems with Go's sync.Map
. Below are the details:
I created a global sync map like:
var MySyncGlobalMap = sync.Map{}
and on an event I populate this map with the intended structure as
map[int64]map[string]interface{}
. So basically I want to populate the sync map with key as int64
and value as another sync map of structure map[string]interface{}
. Below is how I populate the map:
//below is the innerSync map. recSet is returned from DB call in the format : []map[string]interface{}
var innerSyncMap = sync.Map{}
for _, record := range recSet {
sKey := record["key"].(string)
value := record["value"]
innerSyncMap.Store(sKey, value)
}
MySyncGlobalMap.Store(jobID, innerSyncMap)
Now there will be multiple threads which will be accessing this map and do some operations. There will be constant updates to the inner sync map. Once the processing is done on the key of the inner sync map, that key will be deleted from that map.
I will know that a job is complete once the inner sync map becomes empty.
Now since there are multiple threads accessing this map I receiving a panic:
Fatal error: concurrent read and write
I am still wondering that even after using sync map I am facing this issue.
Can any one point out what am I doing wrong?