Created
March 24, 2013 17:42
-
-
Save smagch/5232793 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package simplemap | |
| import ( | |
| "sync" | |
| ) | |
| type Data struct { | |
| i map[string] int | |
| im sync.RWMutex | |
| f map[string] float64 | |
| fm sync.RWMutex | |
| s map[string] string | |
| sm sync.RWMutex | |
| } | |
| func New() *Data { | |
| return new(Data) | |
| } | |
| func (d *Data) SetInt(key string, value int) { | |
| d.im.Lock() | |
| defer d.im.Unlock() | |
| if d.i == nil { | |
| d.i = make(map[string] int) | |
| } | |
| d.i[key] = value | |
| } | |
| func (d *Data) SetFloat(key string, value float64) { | |
| d.fm.Lock() | |
| defer d.fm.Unlock() | |
| if d.f == nil { | |
| d.f = make(map[string] float64) | |
| } | |
| d.f[key] = value | |
| } | |
| func (d *Data) SetString(key string, value string) { | |
| d.sm.Lock() | |
| defer d.sm.Unlock() | |
| if d.s == nil { | |
| d.s = make(map[string] string) | |
| } | |
| d.s[key] = value | |
| } | |
| func (d *Data) GetInt(key string) (int, bool) { | |
| d.im.RLock() | |
| defer d.im.RUnlock() | |
| v, ok := d.i[key] | |
| return v, ok | |
| } | |
| func (d *Data) GetString(key string) (string, bool) { | |
| d.sm.RLock() | |
| defer d.sm.RUnlock() | |
| v, ok := d.s[key] | |
| return v, ok | |
| } | |
| func (d *Data) GetFloat(key string) (float64, bool) { | |
| d.fm.RLock() | |
| defer d.fm.RUnlock() | |
| v, ok := d.f[key] | |
| return v, ok | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment