golang sync.Map 使用
自1.9版本以后提供了sync.Map,支持多线程并发读写,比之前的加锁map性能要好一点。 提供一下几个方法: type Map //删除指定key func (m *Map) Delete(key interface{}) //查询指定key func (m *Map) Load(key interface{}) (value interface{}, ok bool) //查询,查不到则追加 func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) //遍历map func (m *Map) Range(f func(key, value interface{}) bool) //添加 func (m *Map) Store(key, value interface{}) 用例: // main.go package main import ( "fmt" "sync" ) type Class struct { Students sync.Map } func handler(key, value interface{}) bool { fmt.Printf("Name :%s %s\n", key, value) return true } func main()