package mapsync import ( "sync" ) var ( // sync map MapFloat *mapFloat ) // lock type mapFloat struct { sync.RWMutex Data map[string]float64 } func NewMapFloat() *mapFloat { return &mapFloat{ Data: make(map[string]float64), } } func (c *mapFloat) Set(key string, val float64) { c.Lock() defer c.Unlock() c.Data[key] = val } func (c *mapFloat) Get(key string) float64 { c.RLock() defer c.RUnlock() vals, ok := c.Data[key] if !ok { return 0 } return vals } func (c *mapFloat) Del(key string) { c.Lock() defer c.Unlock() delete(c.Data, key) } func (c *mapFloat) Keys() (keys []string) { c.RLock() defer c.RUnlock() for k, _ := range c.Data { keys = append(keys, k) } return } func (c *mapFloat) All() map[string]float64 { c.RLock() defer c.RUnlock() return c.Data }