Skip to content

Instantly share code, notes, and snippets.

@smagch
Created March 24, 2013 17:42
Show Gist options
  • Select an option

  • Save smagch/5232793 to your computer and use it in GitHub Desktop.

Select an option

Save smagch/5232793 to your computer and use it in GitHub Desktop.
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