Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. ~> ccg -f github.com/reusee/cowmap -t Key=string,Value=string -r Map=StrStrMap,New=NewStrStrMap
  2. import "sync"
  3.  
  4. type StrStrMap struct {
  5. v atomic.Value
  6. l sync.Mutex
  7. }
  8.  
  9. func NewStrStrMap(m map[string]string) *StrStrMap {
  10. ret := new(StrStrMap)
  11. ret.v.Store(m)
  12. return ret
  13. }
  14.  
  15. func (m *StrStrMap) Set(key string, value string) {
  16. m.l.Lock()
  17. defer m.l.Unlock()
  18. cur := m.v.Load().(map[string]string)
  19. newMap := make(map[string]string)
  20. for k, v := range cur {
  21. newMap[k] = v
  22. }
  23. newMap[key] = value
  24. m.v.Store(newMap)
  25. }
  26.  
  27. func (m *StrStrMap) Get(key string) string {
  28. cur := m.v.Load().(map[string]string)
  29. return cur[key]
  30. }
  31.  
  32. func (m *StrStrMap) Get2(key string) (value string, ok bool) {
  33. cur := m.v.Load().(map[string]string)
  34. value, ok = cur[key]
  35. return
  36. }
  37.  
  38. func (m *StrStrMap) Del(key string) {
  39. m.l.Lock()
  40. defer m.l.Unlock()
  41. cur := m.v.Load().(map[string]string)
  42. newMap := make(map[string]string)
  43. for k, v := range cur {
  44. newMap[k] = v
  45. }
  46. delete(newMap, key)
  47. m.v.Store(newMap)
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement