When trying to use this struct with multiple goroutines sometimes I get one of these errors:
fatal error: concurrent map read and map write
or
concurrent map writes
After reading the this thread I made sure to return a reference in the constructor and pass in a reference to the receivers.
The entirety of the code where this is being used is in this github repo
type concurrentStorage struct {
sync.Mutex
domain string
urls map[url.URL]bool
}
func newConcurrentStorage(d string) *concurrentStorage{
return &concurrentStorage{
domain: d,
urls: map[url.URL]bool{},
}
}
func (c *concurrentStorage) add(u url.URL) (bool) {
c.Lock()
defer c.Unlock()
if _, ok := c.urls[u]; ok{
return false
}
c.urls[u] = true
return true
}