So currently I've come upon a very real problem in writing "correct" golang. I have an object (for the sake of simplicity lets think of it as a map[string]string) and I want it to hold a "shared" state between multiple gortuines.
Currently the implementation goes something like this:
//Inside shared_state.go
var sharedMap map[string]string = make(map[string]string)
var mutex sync.RWMutex = sync.RWMutex{}
func Add(k string, v string) bool {
mutex.Lock()
if _, exists := sharedMap[k]; exists {
mutex.Unlock()
return false
}
tokenMap[k] = v
mutex.Unlock()
return true
}
//Other methods to access, modify... etc
Whilst this does do the job is quite an ugly implementation by go standards, which encourage modeling concurrency using message.
Are there easy ways of modeling shared state using messages that I am blatantly unaware of ? Or am I forced to use mutexes in this kind of cases ?