Skip to content

Instantly share code, notes, and snippets.

@reusee
Created June 1, 2015 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reusee/44c02070a6987bda4a0a to your computer and use it in GitHub Desktop.
Save reusee/44c02070a6987bda4a0a to your computer and use it in GitHub Desktop.
code gen example
~> 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