代码拉取完成,页面将自动刷新
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{})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。