go_below_19.go 642 B

123456789101112131415161718192021222324252627282930313233
  1. //+build !go1.9
  2. package concurrent
  3. import "sync"
  4. // Map implements a thread safe map for go version below 1.9 using mutex
  5. type Map struct {
  6. lock sync.RWMutex
  7. data map[interface{}]interface{}
  8. }
  9. // NewMap creates a thread safe map
  10. func NewMap() *Map {
  11. return &Map{
  12. data: make(map[interface{}]interface{}, 32),
  13. }
  14. }
  15. // Load is same as sync.Map Load
  16. func (m *Map) Load(key interface{}) (elem interface{}, found bool) {
  17. m.lock.RLock()
  18. elem, found = m.data[key]
  19. m.lock.RUnlock()
  20. return
  21. }
  22. // Load is same as sync.Map Store
  23. func (m *Map) Store(key interface{}, elem interface{}) {
  24. m.lock.Lock()
  25. m.data[key] = elem
  26. m.lock.Unlock()
  27. }