7.21 杭州源创会火热报名中,一起来看看移动开发如何紧跟浪潮?
Watch Star Fork

rushmore / zbus-goGoMIT

加入码云
与超过 300 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
zbus Golang server and client https://zbus.io
一键复制 编辑 原始数据 按行查看 历史
syncmap.go 1.21 KB rushmore 提交于 2017-08-16 19:18 . zbus-go server & client init
package main
import (
"sync"
)
//SyncMap goroutine safe map
//ReadWirte mutex applied, only support string type key
type SyncMap struct {
Map map[string]interface{}
sync.RWMutex
}
//Get by key
func (m *SyncMap) Get(key string) interface{} {
m.RLock()
defer m.RUnlock()
val, ok := m.Map[key]
if !ok {
return nil
}
return val
}
//Set key-value pair
func (m *SyncMap) Set(key string, val interface{}) {
m.Lock()
defer m.Unlock()
m.Map[key] = val
}
//Remove key
func (m *SyncMap) Remove(key string) interface{} {
m.Lock()
defer m.Unlock()
val := m.Map[key]
delete(m.Map, key)
return val
}
//Copy as map[string]string
func (m *SyncMap) Copy() map[string]string {
m.RLock()
defer m.RUnlock()
res := make(map[string]string)
for key, val := range m.Map {
value, ok := val.(string)
if ok {
res[key] = value
}
}
return res
}
//Contains check key exists
func (m *SyncMap) Contains(key string) bool {
m.RLock()
defer m.RUnlock()
_, ok := m.Map[key]
return ok
}
//Size return number of keys in map
func (m *SyncMap) Size() int {
m.RLock()
defer m.RUnlock()
return len(m.Map)
}
//Clear all key-values
func (m *SyncMap) Clear() {
m.Lock()
defer m.Unlock()
m.Map = make(map[string]interface{})
}

评论 ( 0 )

你可以在登录后,发表评论

11_float_left_people 11_float_left_close