Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ~> ccg -f github.com/reusee/cowmap -t Key=string,Value=string -r Map=StrStrMap,New=NewStrStrMap
- import "sync"
- type StrStrMap struct {
- v atomic.Value
- l sync.Mutex
- }
- func NewStrStrMap(m map[string]string) *StrStrMap {
- ret := new(StrStrMap)
- ret.v.Store(m)
- return ret
- }
- func (m *StrStrMap) Set(key string, value string) {
- m.l.Lock()
- defer m.l.Unlock()
- cur := m.v.Load().(map[string]string)
- newMap := make(map[string]string)
- for k, v := range cur {
- newMap[k] = v
- }
- newMap[key] = value
- m.v.Store(newMap)
- }
- func (m *StrStrMap) Get(key string) string {
- cur := m.v.Load().(map[string]string)
- return cur[key]
- }
- func (m *StrStrMap) Get2(key string) (value string, ok bool) {
- cur := m.v.Load().(map[string]string)
- value, ok = cur[key]
- return
- }
- func (m *StrStrMap) Del(key string) {
- m.l.Lock()
- defer m.l.Unlock()
- cur := m.v.Load().(map[string]string)
- newMap := make(map[string]string)
- for k, v := range cur {
- newMap[k] = v
- }
- delete(newMap, key)
- m.v.Store(newMap)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement