前端之家收集整理的这篇文章主要介绍了
golang 1.8 并发安全Map简单实现,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
type SafeMap struct {
sync.RWMutex
Map map[int64]string
}
func NewSafeMap(size int) *SafeMap {
sm := @H_502_15@new(SafeMap)
sm.Map = @H_502_15@make(map[int64]string,size)
return sm
}
func (sm *SafeMap) ReadMap(key int64) string {
sm.RLock()
value := sm.Map[key]
sm.RUnlock()
return value
}
func (sm *SafeMap) WriteMap(key int64,value string) {
sm.Lock()
sm.Map[key] = value
sm.Unlock()
}
func (sm *SafeMap) Keys() []int64 {
sm.RLock()
value := @H_502_15@make([]int64, 0)
for k,_ := range sm.Map {
value = @H_502_15@append(value,k)
}
sm.RUnlock()
return value
}