Created
June 1, 2015 12:22
-
-
Save reusee/44c02070a6987bda4a0a to your computer and use it in GitHub Desktop.
code gen example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~> 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment