diff --git a/g/container/glist/safelist.go b/g/container/glist/safelist.go index c1a241f8c..d029555dc 100644 --- a/g/container/glist/safelist.go +++ b/g/container/glist/safelist.go @@ -15,87 +15,87 @@ import ( // 变长双向链表 type SafeList struct { - sync.RWMutex - L *list.List + mu sync.RWMutex + list *list.List } // 获得一个变长链表指针 func NewSafeList() *SafeList { - return &SafeList{L: list.New()} + return &SafeList{list: list.New()} } // 往链表头入栈数据项 func (this *SafeList) PushFront(v interface{}) *list.Element { - this.Lock() - e := this.L.PushFront(v) - this.Unlock() + this.mu.Lock() + e := this.list.PushFront(v) + this.mu.Unlock() return e } // 往链表尾入栈数据项 func (this *SafeList) PushBack(v interface{}) *list.Element { - this.Lock() - r := this.L.PushBack(v) - this.Unlock() + this.mu.Lock() + r := this.list.PushBack(v) + this.mu.Unlock() return r } // 在list 中元素mark之后插入一个值为v的元素,并返回该元素,如果mark不是list中元素,则list不改变。 func (this *SafeList) InsertAfter(v interface{}, mark *list.Element) *list.Element { - this.Lock() - r := this.L.InsertAfter(v, mark) - this.Unlock() + this.mu.Lock() + r := this.list.InsertAfter(v, mark) + this.mu.Unlock() return r } // 在list 中元素mark之前插入一个值为v的元素,并返回该元素,如果mark不是list中元素,则list不改变。 func (this *SafeList) InsertBefore(v interface{}, mark *list.Element) *list.Element { - this.Lock() - r := this.L.InsertBefore(v, mark) - this.Unlock() + this.mu.Lock() + r := this.list.InsertBefore(v, mark) + this.mu.Unlock() return r } // 批量往链表头入栈数据项 func (this *SafeList) BatchPushFront(vs []interface{}) { - this.Lock() + this.mu.Lock() for _, item := range vs { - this.L.PushFront(item) + this.list.PushFront(item) } - this.Unlock() + this.mu.Unlock() } // 从链表尾端出栈数据项(删除) func (this *SafeList) PopBack() interface{} { - this.Lock() - if elem := this.L.Back(); elem != nil { - item := this.L.Remove(elem) - this.Unlock() + this.mu.Lock() + if elem := this.list.Back(); elem != nil { + item := this.list.Remove(elem) + this.mu.Unlock() return item } - this.Unlock() + this.mu.Unlock() return nil } // 从链表头端出栈数据项(删除) func (this *SafeList) PopFront() interface{} { - this.Lock() - if elem := this.L.Front(); elem != nil { - item := this.L.Remove(elem) - this.Unlock() + this.mu.Lock() + if elem := this.list.Front(); elem != nil { + item := this.list.Remove(elem) + this.mu.Unlock() return item } - this.Unlock() + this.mu.Unlock() return nil } // 批量从链表尾端出栈数据项(删除) func (this *SafeList) BatchPopBack(max int) []interface{} { - this.Lock() - count := this.L.Len() + this.mu.Lock() + count := this.list.Len() if count == 0 { - this.Unlock() + this.mu.Unlock() return []interface{}{} } @@ -104,18 +104,18 @@ func (this *SafeList) BatchPopBack(max int) []interface{} { } items := make([]interface{}, count) for i := 0; i < count; i++ { - items[i] = this.L.Remove(this.L.Back()) + items[i] = this.list.Remove(this.list.Back()) } - this.Unlock() + this.mu.Unlock() return items } // 批量从链表头端出栈数据项(删除) func (this *SafeList) BatchPopFront(max int) []interface{} { - this.Lock() - count := this.L.Len() + this.mu.Lock() + count := this.list.Len() if count == 0 { - this.Unlock() + this.mu.Unlock() return []interface{}{} } @@ -124,137 +124,137 @@ func (this *SafeList) BatchPopFront(max int) []interface{} { } items := make([]interface{}, count) for i := 0; i < count; i++ { - items[i] = this.L.Remove(this.L.Front()) + items[i] = this.list.Remove(this.list.Front()) } - this.Unlock() + this.mu.Unlock() return items } // 批量从链表尾端依次获取所有数据(删除) func (this *SafeList) PopBackAll() []interface{} { - this.Lock() - count := this.L.Len() + this.mu.Lock() + count := this.list.Len() if count == 0 { - this.Unlock() + this.mu.Unlock() return []interface{}{} } items := make([]interface{}, count) for i := 0; i < count; i++ { - items[i] = this.L.Remove(this.L.Back()) + items[i] = this.list.Remove(this.list.Back()) } - this.Unlock() + this.mu.Unlock() return items } // 批量从链表头端依次获取所有数据(删除) func (this *SafeList) PopFrontAll() []interface{} { - this.Lock() - count := this.L.Len() + this.mu.Lock() + count := this.list.Len() if count == 0 { - this.Unlock() + this.mu.Unlock() return []interface{}{} } items := make([]interface{}, count) for i := 0; i < count; i++ { - items[i] = this.L.Remove(this.L.Front()) + items[i] = this.list.Remove(this.list.Front()) } - this.Unlock() + this.mu.Unlock() return items } // 删除数据项 func (this *SafeList) Remove(e *list.Element) interface{} { - this.Lock() - r := this.L.Remove(e) - this.Unlock() + this.mu.Lock() + r := this.list.Remove(e) + this.mu.Unlock() return r } // 删除所有数据项 func (this *SafeList) RemoveAll() { - this.Lock() - this.L = list.New() - this.Unlock() + this.mu.Lock() + this.list = list.New() + this.mu.Unlock() } // 从链表头获取所有数据(不删除) func (this *SafeList) FrontAll() []interface{} { - this.RLock() - count := this.L.Len() + this.mu.RLock() + count := this.list.Len() if count == 0 { - this.RUnlock() + this.mu.RUnlock() return []interface{}{} } items := make([]interface{}, 0, count) - for e := this.L.Front(); e != nil; e = e.Next() { + for e := this.list.Front(); e != nil; e = e.Next() { items = append(items, e.Value) } - this.RUnlock() + this.mu.RUnlock() return items } // 从链表尾获取所有数据(不删除) func (this *SafeList) BackAll() []interface{} { - this.RLock() - count := this.L.Len() + this.mu.RLock() + count := this.list.Len() if count == 0 { - this.RUnlock() + this.mu.RUnlock() return []interface{}{} } items := make([]interface{}, 0, count) - for e := this.L.Back(); e != nil; e = e.Prev() { + for e := this.list.Back(); e != nil; e = e.Prev() { items = append(items, e.Value) } - this.RUnlock() + this.mu.RUnlock() return items } // 获取链表头值(不删除) func (this *SafeList) FrontItem() interface{} { - this.RLock() - if f := this.L.Front(); f != nil { - this.RUnlock() + this.mu.RLock() + if f := this.list.Front(); f != nil { + this.mu.RUnlock() return f.Value } - this.RUnlock() + this.mu.RUnlock() return nil } // 获取链表尾值(不删除) func (this *SafeList) BackItem() interface{} { - this.RLock() - if f := this.L.Back(); f != nil { - this.RUnlock() + this.mu.RLock() + if f := this.list.Back(); f != nil { + this.mu.RUnlock() return f.Value } - this.RUnlock() + this.mu.RUnlock() return nil } // 获取表头指针 func (this *SafeList) Front() *list.Element { - this.RLock() - r := this.L.Front() - this.RUnlock() + this.mu.RLock() + r := this.list.Front() + this.mu.RUnlock() return r } // 获取表位指针 func (this *SafeList) Back() *list.Element { - this.RLock() - r := this.L.Back() - this.RUnlock() + this.mu.RLock() + r := this.list.Back() + this.mu.RUnlock() return r } // 获取链表长度 func (this *SafeList) Len() int { - this.RLock() - length := this.L.Len() - this.RUnlock() + this.mu.RLock() + length := this.list.Len() + this.mu.RUnlock() return length } diff --git a/g/container/gmap/int_bool_map.go b/g/container/gmap/int_bool_map.go index f5d1529ac..a5c2bb526 100644 --- a/g/container/gmap/int_bool_map.go +++ b/g/container/gmap/int_bool_map.go @@ -12,8 +12,8 @@ import ( ) type IntBoolMap struct { - sync.RWMutex - m map[int]bool + mu sync.RWMutex + m map[int]bool } func NewIntBoolMap() *IntBoolMap { @@ -25,115 +25,115 @@ func NewIntBoolMap() *IntBoolMap { // 哈希表克隆 func (this *IntBoolMap) Clone() *map[int]bool { m := make(map[int]bool) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *IntBoolMap) Set(key int, val bool) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *IntBoolMap) BatchSet(m map[int]bool) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *IntBoolMap) Get(key int) (bool) { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } // 删除键值对 func (this *IntBoolMap) Remove(key int) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *IntBoolMap) BatchRemove(keys []int) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *IntBoolMap) GetAndRemove(key int) (bool) { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *IntBoolMap) Keys() []int { - this.RLock() + this.mu.RLock() keys := make([]int, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) //func (this *IntBoolMap) Values() []bool { -// this.RLock() +// this.mu.RLock() // vals := make([]bool, 0) // for _, val := range this.m { // vals = append(vals, val) // } -// this.RUnlock() +// this.mu.RUnlock() // return vals //} // 是否存在某个键 func (this *IntBoolMap) Contains(key int) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *IntBoolMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *IntBoolMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *IntBoolMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[int]bool) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/int_int_map.go b/g/container/gmap/int_int_map.go index 8dcba4f57..de2bdde08 100644 --- a/g/container/gmap/int_int_map.go +++ b/g/container/gmap/int_int_map.go @@ -12,8 +12,8 @@ import ( ) type IntIntMap struct { - sync.RWMutex - m map[int]int + mu sync.RWMutex + m map[int]int } func NewIntIntMap() *IntIntMap { @@ -25,115 +25,115 @@ func NewIntIntMap() *IntIntMap { // 哈希表克隆 func (this *IntIntMap) Clone() *map[int]int { m := make(map[int]int) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *IntIntMap) Set(key int, val int) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *IntIntMap) BatchSet(m map[int]int) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *IntIntMap) Get(key int) (int) { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } // 删除键值对 func (this *IntIntMap) Remove(key int) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *IntIntMap) BatchRemove(keys []int) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *IntIntMap) GetAndRemove(key int) (int) { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *IntIntMap) Keys() []int { - this.RLock() + this.mu.RLock() keys := make([]int, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) func (this *IntIntMap) Values() []int { - this.RLock() + this.mu.RLock() vals := make([]int, 0) for _, val := range this.m { vals = append(vals, val) } - this.RUnlock() + this.mu.RUnlock() return vals } // 是否存在某个键 func (this *IntIntMap) Contains(key int) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *IntIntMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *IntIntMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *IntIntMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[int]int) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/int_interface_map.go b/g/container/gmap/int_interface_map.go index da355782c..cbb4bc351 100644 --- a/g/container/gmap/int_interface_map.go +++ b/g/container/gmap/int_interface_map.go @@ -13,8 +13,8 @@ import ( ) type IntInterfaceMap struct { - sync.RWMutex - m map[int]interface{} + mu sync.RWMutex + m map[int]interface{} } func NewIntInterfaceMap() *IntInterfaceMap { @@ -26,139 +26,139 @@ func NewIntInterfaceMap() *IntInterfaceMap { // 哈希表克隆 func (this *IntInterfaceMap) Clone() *map[int]interface{} { m := make(map[int]interface{}) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *IntInterfaceMap) Set(key int, val interface{}) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *IntInterfaceMap) BatchSet(m map[int]interface{}) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *IntInterfaceMap) Get(key int) (interface{}) { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } func (this *IntInterfaceMap) GetBool(key int) bool { - return gconv.Bool(this.Get(key)) + return gconv.Bool(this.mu.Get(key)) } func (this *IntInterfaceMap) GetInt(key int) int { - return gconv.Int(this.Get(key)) + return gconv.Int(this.mu.Get(key)) } func (this *IntInterfaceMap) GetUint (key int) uint { - return gconv.Uint(this.Get(key)) + return gconv.Uint(this.mu.Get(key)) } func (this *IntInterfaceMap) GetFloat32 (key int) float32 { - return gconv.Float32(this.Get(key)) + return gconv.Float32(this.mu.Get(key)) } func (this *IntInterfaceMap) GetFloat64 (key int) float64 { - return gconv.Float64(this.Get(key)) + return gconv.Float64(this.mu.Get(key)) } func (this *IntInterfaceMap) GetString (key int) string { - return gconv.String(this.Get(key)) + return gconv.String(this.mu.Get(key)) } // 删除键值对 func (this *IntInterfaceMap) Remove(key int) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *IntInterfaceMap) BatchRemove(keys []int) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *IntInterfaceMap) GetAndRemove(key int) (interface{}) { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *IntInterfaceMap) Keys() []int { - this.RLock() + this.mu.RLock() keys := make([]int, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) func (this *IntInterfaceMap) Values() []interface{} { - this.RLock() + this.mu.RLock() vals := make([]interface{}, 0) for _, val := range this.m { vals = append(vals, val) } - this.RUnlock() + this.mu.RUnlock() return vals } // 是否存在某个键 func (this *IntInterfaceMap) Contains(key int) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *IntInterfaceMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *IntInterfaceMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *IntInterfaceMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[int]interface{}) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/int_string_map.go b/g/container/gmap/int_string_map.go index e9f3d08c1..7b544bc3a 100644 --- a/g/container/gmap/int_string_map.go +++ b/g/container/gmap/int_string_map.go @@ -12,8 +12,8 @@ import ( ) type IntStringMap struct { - sync.RWMutex - m map[int]string + mu sync.RWMutex + m map[int]string } func NewIntStringMap() *IntStringMap { @@ -25,115 +25,115 @@ func NewIntStringMap() *IntStringMap { // 哈希表克隆 func (this *IntStringMap) Clone() *map[int]string { m := make(map[int]string) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *IntStringMap) Set(key int, val string) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *IntStringMap) BatchSet(m map[int]string) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *IntStringMap) Get(key int) (string) { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } // 删除键值对 func (this *IntStringMap) Remove(key int) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *IntStringMap) BatchRemove(keys []int) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *IntStringMap) GetAndRemove(key int) (string) { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *IntStringMap) Keys() []int { - this.RLock() + this.mu.RLock() keys := make([]int, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) func (this *IntStringMap) Values() []string { - this.RLock() + this.mu.RLock() vals := make([]string, 0) for _, val := range this.m { vals = append(vals, val) } - this.RUnlock() + this.mu.RUnlock() return vals } // 是否存在某个键 func (this *IntStringMap) Contains(key int) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *IntStringMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *IntStringMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *IntStringMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[int]string) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/interface_interface_map.go b/g/container/gmap/interface_interface_map.go index ae46ced9a..f5162e07e 100644 --- a/g/container/gmap/interface_interface_map.go +++ b/g/container/gmap/interface_interface_map.go @@ -13,8 +13,8 @@ import ( ) type InterfaceInterfaceMap struct { - sync.RWMutex - m map[interface{}]interface{} + mu sync.RWMutex + m map[interface{}]interface{} } func NewInterfaceInterfaceMap() *InterfaceInterfaceMap { @@ -26,138 +26,138 @@ func NewInterfaceInterfaceMap() *InterfaceInterfaceMap { // 哈希表克隆 func (this *InterfaceInterfaceMap) Clone() *map[interface{}]interface{} { m := make(map[interface{}]interface{}) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *InterfaceInterfaceMap) Set(key interface{}, val interface{}) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *InterfaceInterfaceMap) BatchSet(m map[interface{}]interface{}) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *InterfaceInterfaceMap) Get(key interface{}) (interface{}) { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } func (this *InterfaceInterfaceMap) GetBool(key interface{}) bool { - return gconv.Bool(this.Get(key)) + return gconv.Bool(this.mu.Get(key)) } func (this *InterfaceInterfaceMap) GetInt(key interface{}) int { - return gconv.Int(this.Get(key)) + return gconv.Int(this.mu.Get(key)) } func (this *InterfaceInterfaceMap) GetUint (key interface{}) uint { - return gconv.Uint(this.Get(key)) + return gconv.Uint(this.mu.Get(key)) } func (this *InterfaceInterfaceMap) GetFloat32 (key interface{}) float32 { - return gconv.Float32(this.Get(key)) + return gconv.Float32(this.mu.Get(key)) } func (this *InterfaceInterfaceMap) GetFloat64 (key interface{}) float64 { - return gconv.Float64(this.Get(key)) + return gconv.Float64(this.mu.Get(key)) } func (this *InterfaceInterfaceMap) GetString (key interface{}) string { - return gconv.String(this.Get(key)) + return gconv.String(this.mu.Get(key)) } // 删除键值对 func (this *InterfaceInterfaceMap) Remove(key interface{}) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *InterfaceInterfaceMap) BatchRemove(keys []interface{}) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *InterfaceInterfaceMap) GetAndRemove(key interface{}) (interface{}) { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *InterfaceInterfaceMap) Keys() []interface{} { - this.RLock() + this.mu.RLock() keys := make([]interface{}, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) func (this *InterfaceInterfaceMap) Values() []interface{} { - this.RLock() + this.mu.RLock() vals := make([]interface{}, 0) for _, val := range this.m { vals = append(vals, val) } - this.RUnlock() + this.mu.RUnlock() return vals } // 是否存在某个键 func (this *InterfaceInterfaceMap) Contains(key interface{}) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *InterfaceInterfaceMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *InterfaceInterfaceMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *InterfaceInterfaceMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[interface{}]interface{}) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/string_bool_map.go b/g/container/gmap/string_bool_map.go index d78d5a546..3623cc798 100644 --- a/g/container/gmap/string_bool_map.go +++ b/g/container/gmap/string_bool_map.go @@ -12,8 +12,8 @@ import ( ) type StringBoolMap struct { - sync.RWMutex - m map[string]bool + mu sync.RWMutex + m map[string]bool } func NewStringBoolMap() *StringBoolMap { @@ -25,114 +25,114 @@ func NewStringBoolMap() *StringBoolMap { // 哈希表克隆 func (this *StringBoolMap) Clone() *map[string]bool { m := make(map[string]bool) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *StringBoolMap) Set(key string, val bool) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *StringBoolMap) BatchSet(m map[string]bool) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *StringBoolMap) Get(key string) (bool) { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } // 删除键值对 func (this *StringBoolMap) Remove(key string) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *StringBoolMap) BatchRemove(keys []string) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *StringBoolMap) GetAndRemove(key string) (bool) { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *StringBoolMap) Keys() []string { - this.RLock() + this.mu.RLock() keys := make([]string, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) //func (this *StringBoolMap) Values() []bool { -// this.RLock() +// this.mu.RLock() // vals := make([]bool, 0) // for _, val := range this.m { // vals = append(vals, val) // } -// this.RUnlock() +// this.mu.RUnlock() // return vals //} // 是否存在某个键 func (this *StringBoolMap) Contains(key string) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *StringBoolMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *StringBoolMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *StringBoolMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[string]bool) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/string_int_map.go b/g/container/gmap/string_int_map.go index c8c1df5f0..9dd20f224 100644 --- a/g/container/gmap/string_int_map.go +++ b/g/container/gmap/string_int_map.go @@ -12,8 +12,8 @@ import ( ) type StringIntMap struct { - sync.RWMutex - m map[string]int + mu sync.RWMutex + m map[string]int } func NewStringIntMap() *StringIntMap { @@ -25,115 +25,115 @@ func NewStringIntMap() *StringIntMap { // 哈希表克隆 func (this *StringIntMap) Clone() *map[string]int { m := make(map[string]int) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *StringIntMap) Set(key string, val int) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *StringIntMap) BatchSet(m map[string]int) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *StringIntMap) Get(key string) (int) { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } // 删除键值对 func (this *StringIntMap) Remove(key string) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *StringIntMap) BatchRemove(keys []string) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *StringIntMap) GetAndRemove(key string) (int) { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *StringIntMap) Keys() []string { - this.RLock() + this.mu.RLock() keys := make([]string, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) func (this *StringIntMap) Values() []int { - this.RLock() + this.mu.RLock() vals := make([]int, 0) for _, val := range this.m { vals = append(vals, val) } - this.RUnlock() + this.mu.RUnlock() return vals } // 是否存在某个键 func (this *StringIntMap) Contains(key string) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *StringIntMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *StringIntMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *StringIntMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[string]int) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/string_interface_map.go b/g/container/gmap/string_interface_map.go index 2c5ebbc3d..740738b4e 100644 --- a/g/container/gmap/string_interface_map.go +++ b/g/container/gmap/string_interface_map.go @@ -13,8 +13,8 @@ import ( ) type StringInterfaceMap struct { - sync.RWMutex - m map[string]interface{} + mu sync.RWMutex + m map[string]interface{} } func NewStringInterfaceMap() *StringInterfaceMap { @@ -26,138 +26,138 @@ func NewStringInterfaceMap() *StringInterfaceMap { // 哈希表克隆 func (this *StringInterfaceMap) Clone() *map[string]interface{} { m := make(map[string]interface{}) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *StringInterfaceMap) Set(key string, val interface{}) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *StringInterfaceMap) BatchSet(m map[string]interface{}) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *StringInterfaceMap) Get(key string) interface{} { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } func (this *StringInterfaceMap) GetBool(key string) bool { - return gconv.Bool(this.Get(key)) + return gconv.Bool(this.mu.Get(key)) } func (this *StringInterfaceMap) GetInt(key string) int { - return gconv.Int(this.Get(key)) + return gconv.Int(this.mu.Get(key)) } func (this *StringInterfaceMap) GetUint (key string) uint { - return gconv.Uint(this.Get(key)) + return gconv.Uint(this.mu.Get(key)) } func (this *StringInterfaceMap) GetFloat32 (key string) float32 { - return gconv.Float32(this.Get(key)) + return gconv.Float32(this.mu.Get(key)) } func (this *StringInterfaceMap) GetFloat64 (key string) float64 { - return gconv.Float64(this.Get(key)) + return gconv.Float64(this.mu.Get(key)) } func (this *StringInterfaceMap) GetString (key string) string { - return gconv.String(this.Get(key)) + return gconv.String(this.mu.Get(key)) } // 删除键值对 func (this *StringInterfaceMap) Remove(key string) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *StringInterfaceMap) BatchRemove(keys []string) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *StringInterfaceMap) GetAndRemove(key string) interface{} { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *StringInterfaceMap) Keys() []string { - this.RLock() + this.mu.RLock() keys := make([]string, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) func (this *StringInterfaceMap) Values() []interface{} { - this.RLock() + this.mu.RLock() vals := make([]interface{}, 0) for _, val := range this.m { vals = append(vals, val) } - this.RUnlock() + this.mu.RUnlock() return vals } // 是否存在某个键 func (this *StringInterfaceMap) Contains(key string) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *StringInterfaceMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *StringInterfaceMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *StringInterfaceMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[string]interface{}) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/string_string_map.go b/g/container/gmap/string_string_map.go index b8a1dbd8a..3a1f68d6e 100644 --- a/g/container/gmap/string_string_map.go +++ b/g/container/gmap/string_string_map.go @@ -12,8 +12,8 @@ import ( ) type StringStringMap struct { - sync.RWMutex - m map[string]string + mu sync.RWMutex + m map[string]string } func NewStringStringMap() *StringStringMap { @@ -25,114 +25,114 @@ func NewStringStringMap() *StringStringMap { // 哈希表克隆 func (this *StringStringMap) Clone() *map[string]string { m := make(map[string]string) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *StringStringMap) Set(key string, val string) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *StringStringMap) BatchSet(m map[string]string) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *StringStringMap) Get(key string) string { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } // 删除键值对 func (this *StringStringMap) Remove(key string) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *StringStringMap) BatchRemove(keys []string) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *StringStringMap) GetAndRemove(key string) string { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *StringStringMap) Keys() []string { - this.RLock() + this.mu.RLock() keys := make([]string, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) func (this *StringStringMap) Values() []string { - this.RLock() + this.mu.RLock() vals := make([]string, 0) for _, val := range this.m { vals = append(vals, val) } - this.RUnlock() + this.mu.RUnlock() return vals } // 是否存在某个键 func (this *StringStringMap) Contains(key string) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *StringStringMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *StringStringMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *StringStringMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[string]string) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gmap/uint_interface_map.go b/g/container/gmap/uint_interface_map.go index 0c8e6af96..baa0ed728 100644 --- a/g/container/gmap/uint_interface_map.go +++ b/g/container/gmap/uint_interface_map.go @@ -13,8 +13,8 @@ import ( ) type UintInterfaceMap struct { - sync.RWMutex - m map[uint]interface{} + mu sync.RWMutex + m map[uint]interface{} } func NewUintInterfaceMap() *UintInterfaceMap { @@ -26,139 +26,139 @@ func NewUintInterfaceMap() *UintInterfaceMap { // 哈希表克隆 func (this *UintInterfaceMap) Clone() *map[uint]interface{} { m := make(map[uint]interface{}) - this.RLock() + this.mu.RLock() for k, v := range this.m { m[k] = v } - this.RUnlock() + this.mu.RUnlock() return &m } // 设置键值对 func (this *UintInterfaceMap) Set(key uint, val interface{}) { - this.Lock() + this.mu.Lock() this.m[key] = val - this.Unlock() + this.mu.Unlock() } // 批量设置键值对 func (this *UintInterfaceMap) BatchSet(m map[uint]interface{}) { - this.Lock() + this.mu.Lock() for k, v := range m { this.m[k] = v } - this.Unlock() + this.mu.Unlock() } // 获取键值 func (this *UintInterfaceMap) Get(key uint) (interface{}) { - this.RLock() + this.mu.RLock() val, _ := this.m[key] - this.RUnlock() + this.mu.RUnlock() return val } func (this *UintInterfaceMap) GetBool(key uint) bool { - return gconv.Bool(this.Get(key)) + return gconv.Bool(this.mu.Get(key)) } func (this *UintInterfaceMap) GetInt(key uint) int { - return gconv.Int(this.Get(key)) + return gconv.Int(this.mu.Get(key)) } func (this *UintInterfaceMap) GetUint (key uint) uint { - return gconv.Uint(this.Get(key)) + return gconv.Uint(this.mu.Get(key)) } func (this *UintInterfaceMap) GetFloat32 (key uint) float32 { - return gconv.Float32(this.Get(key)) + return gconv.Float32(this.mu.Get(key)) } func (this *UintInterfaceMap) GetFloat64 (key uint) float64 { - return gconv.Float64(this.Get(key)) + return gconv.Float64(this.mu.Get(key)) } func (this *UintInterfaceMap) GetString (key uint) string { - return gconv.String(this.Get(key)) + return gconv.String(this.mu.Get(key)) } // 删除键值对 func (this *UintInterfaceMap) Remove(key uint) { - this.Lock() + this.mu.Lock() delete(this.m, key) - this.Unlock() + this.mu.Unlock() } // 批量删除键值对 func (this *UintInterfaceMap) BatchRemove(keys []uint) { - this.Lock() + this.mu.Lock() for _, key := range keys { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() } // 返回对应的键值,并删除该键值 func (this *UintInterfaceMap) GetAndRemove(key uint) (interface{}) { - this.Lock() + this.mu.Lock() val, exists := this.m[key] if exists { delete(this.m, key) } - this.Unlock() + this.mu.Unlock() return val } // 返回键列表 func (this *UintInterfaceMap) Keys() []uint { - this.RLock() + this.mu.RLock() keys := make([]uint, 0) for key, _ := range this.m { keys = append(keys, key) } - this.RUnlock() + this.mu.RUnlock() return keys } // 返回值列表(注意是随机排序) func (this *UintInterfaceMap) Values() []interface{} { - this.RLock() + this.mu.RLock() vals := make([]interface{}, 0) for _, val := range this.m { vals = append(vals, val) } - this.RUnlock() + this.mu.RUnlock() return vals } // 是否存在某个键 func (this *UintInterfaceMap) Contains(key uint) bool { - this.RLock() + this.mu.RLock() _, exists := this.m[key] - this.RUnlock() + this.mu.RUnlock() return exists } // 哈希表大小 func (this *UintInterfaceMap) Size() int { - this.RLock() + this.mu.RLock() len := len(this.m) - this.RUnlock() + this.mu.RUnlock() return len } // 哈希表是否为空 func (this *UintInterfaceMap) IsEmpty() bool { - this.RLock() + this.mu.RLock() empty := (len(this.m) == 0) - this.RUnlock() + this.mu.RUnlock() return empty } // 清空哈希表 func (this *UintInterfaceMap) Clear() { - this.Lock() + this.mu.Lock() this.m = make(map[uint]interface{}) - this.Unlock() + this.mu.Unlock() } diff --git a/g/container/gset/int_set.go b/g/container/gset/int_set.go index e0a31e147..470852477 100644 --- a/g/container/gset/int_set.go +++ b/g/container/gset/int_set.go @@ -14,21 +14,21 @@ import ( ) type IntSet struct { - sync.RWMutex - M map[int]struct{} + mu sync.RWMutex + m map[int]struct{} } func NewIntSet() *IntSet { - return &IntSet{M: make(map[int]struct{})} + return &IntSet{m: make(map[int]struct{})} } // 给定回调函数对原始内容进行遍历 func (this *IntSet) Iterator(f func (v int)) { - this.RLock() - for k, _ := range this.M { + this.mu.RLock() + for k, _ := range this.m { f(k) } - this.RUnlock() + this.mu.RUnlock() } // 设置键 @@ -36,9 +36,9 @@ func (this *IntSet) Add(item int) *IntSet { if this.Contains(item) { return this } - this.Lock() - this.M[item] = struct{}{} - this.Unlock() + this.mu.Lock() + this.m[item] = struct{}{} + this.mu.Unlock() return this } @@ -50,76 +50,75 @@ func (this *IntSet) BatchAdd(items []int) *IntSet { } todo := make([]int, 0, count) - this.RLock() + this.mu.RLock() for i := 0; i < count; i++ { - _, exists := this.M[items[i]] + _, exists := this.m[items[i]] if exists { continue } todo = append(todo, items[i]) } - this.RUnlock() + this.mu.RUnlock() count = len(todo) if count == 0 { return this } - this.Lock() + this.mu.Lock() for i := 0; i < count; i++ { - this.M[todo[i]] = struct{}{} + this.m[todo[i]] = struct{}{} } - this.Unlock() + this.mu.Unlock() return this } // 键是否存在 func (this *IntSet) Contains(item int) bool { - this.RLock() - _, exists := this.M[item] - this.RUnlock() + this.mu.RLock() + _, exists := this.m[item] + this.mu.RUnlock() return exists } // 删除键值对 func (this *IntSet) Remove(key int) { - this.Lock() - delete(this.M, key) - this.Unlock() + this.mu.Lock() + delete(this.m, key) + this.mu.Unlock() } // 大小 func (this *IntSet) Size() int { - this.RLock() - l := len(this.M) - this.RUnlock() + this.mu.RLock() + l := len(this.m) + this.mu.RUnlock() return l } // 清空set func (this *IntSet) Clear() { - this.Lock() - this.M = make(map[int]struct{}) - this.Unlock() + this.mu.Lock() + this.m = make(map[int]struct{}) + this.mu.Unlock() } // 转换为数组 func (this *IntSet) Slice() []int { - this.RLock() - ret := make([]int, len(this.M)) + this.mu.RLock() + ret := make([]int, len(this.m)) i := 0 - for item := range this.M { + for item := range this.m { ret[i] = item i++ } - this.RUnlock() + this.mu.RUnlock() return ret } // 转换为字符串 func (this *IntSet) String() string { - s := this.Slice() - return fmt.Sprint(s) + return fmt.Sprint(this.Slice()) } diff --git a/g/container/gset/interface_set.go b/g/container/gset/interface_set.go index 53416f956..85f5a482d 100644 --- a/g/container/gset/interface_set.go +++ b/g/container/gset/interface_set.go @@ -13,21 +13,21 @@ import ( ) type InterfaceSet struct { - sync.RWMutex - M map[interface{}]struct{} + mu sync.RWMutex + m map[interface{}]struct{} } func NewInterfaceSet() *InterfaceSet { - return &InterfaceSet{M: make(map[interface{}]struct{})} + return &InterfaceSet{m: make(map[interface{}]struct{})} } // 给定回调函数对原始内容进行遍历 func (this *InterfaceSet) Iterator(f func (v interface{})) { - this.RLock() - for k, _ := range this.M { + this.mu.RLock() + for k, _ := range this.m { f(k) } - this.RUnlock() + this.mu.RUnlock() } // 添加 @@ -35,9 +35,9 @@ func (this *InterfaceSet) Add(item interface{}) *InterfaceSet { if this.Contains(item) { return this } - this.Lock() - this.M[item] = struct{}{} - this.Unlock() + this.mu.Lock() + this.m[item] = struct{}{} + this.mu.Unlock() return this } @@ -49,75 +49,74 @@ func (this *InterfaceSet) BatchAdd(items []interface{}) *InterfaceSet { } todo := make([]interface{}, 0, count) - this.RLock() + this.mu.RLock() for i := 0; i < count; i++ { - _, exists := this.M[items[i]] + _, exists := this.m[items[i]] if exists { continue } todo = append(todo, items[i]) } - this.RUnlock() + this.mu.RUnlock() count = len(todo) if count == 0 { return this } - this.Lock() + this.mu.Lock() for i := 0; i < count; i++ { - this.M[todo[i]] = struct{}{} + this.m[todo[i]] = struct{}{} } - this.Unlock() + this.mu.Unlock() return this } // 键是否存在 func (this *InterfaceSet) Contains(item interface{}) bool { - this.RLock() - _, exists := this.M[item] - this.RUnlock() + this.mu.RLock() + _, exists := this.m[item] + this.mu.RUnlock() return exists } // 删除键值对 func (this *InterfaceSet) Remove(key interface{}) { - this.Lock() - delete(this.M, key) - this.Unlock() + this.mu.Lock() + delete(this.m, key) + this.mu.Unlock() } // 大小 func (this *InterfaceSet) Size() int { - this.RLock() - l := len(this.M) - this.RUnlock() + this.mu.RLock() + l := len(this.m) + this.mu.RUnlock() return l } // 清空set func (this *InterfaceSet) Clear() { - this.Lock() - this.M = make(map[interface{}]struct{}) - this.Unlock() + this.mu.Lock() + this.m = make(map[interface{}]struct{}) + this.mu.Unlock() } // 转换为数组 func (this *InterfaceSet) Slice() []interface{} { - this.RLock() + this.mu.RLock() i := 0 - ret := make([]interface{}, len(this.M)) - for item := range this.M { + ret := make([]interface{}, len(this.m)) + for item := range this.m { ret[i] = item i++ } - this.RUnlock() + this.mu.RUnlock() return ret } // 转换为字符串 func (this *InterfaceSet) String() string { - s := this.Slice() - return fmt.Sprint(s) + return fmt.Sprint(this.Slice()) } diff --git a/g/container/gset/string_set.go b/g/container/gset/string_set.go index d16ed21e7..ec2c1fbf4 100644 --- a/g/container/gset/string_set.go +++ b/g/container/gset/string_set.go @@ -13,21 +13,21 @@ import ( ) type StringSet struct { - sync.RWMutex - M map[string]struct{} + mu sync.RWMutex + m map[string]struct{} } func NewStringSet() *StringSet { - return &StringSet{M: make(map[string]struct{})} + return &StringSet{m: make(map[string]struct{})} } // 给定回调函数对原始内容进行遍历 func (this *StringSet) Iterator(f func (v string)) { - this.RLock() - for k, _ := range this.M { + this.mu.RLock() + for k, _ := range this.m { f(k) } - this.RUnlock() + this.mu.RUnlock() } // 设置键 @@ -35,9 +35,9 @@ func (this *StringSet) Add(item string) *StringSet { if this.Contains(item) { return this } - this.Lock() - this.M[item] = struct{}{} - this.Unlock() + this.mu.Lock() + this.m[item] = struct{}{} + this.mu.Unlock() return this } @@ -49,76 +49,75 @@ func (this *StringSet) BatchAdd(items []string) *StringSet { } todo := make([]string, 0, count) - this.RLock() + this.mu.RLock() for i := 0; i < count; i++ { - _, exists := this.M[items[i]] + _, exists := this.m[items[i]] if exists { continue } todo = append(todo, items[i]) } - this.RUnlock() + this.mu.RUnlock() count = len(todo) if count == 0 { return this } - this.Lock() + this.mu.Lock() for i := 0; i < count; i++ { - this.M[todo[i]] = struct{}{} + this.m[todo[i]] = struct{}{} } - this.Unlock() + this.mu.Unlock() return this } // 键是否存在 func (this *StringSet) Contains(item string) bool { - this.RLock() - _, exists := this.M[item] - this.RUnlock() + this.mu.RLock() + _, exists := this.m[item] + this.mu.RUnlock() return exists } // 删除键值对 func (this *StringSet) Remove(key string) { - this.Lock() - delete(this.M, key) - this.Unlock() + this.mu.Lock() + delete(this.m, key) + this.mu.Unlock() } // 大小 func (this *StringSet) Size() int { - this.RLock() - l := len(this.M) - this.RUnlock() + this.mu.RLock() + l := len(this.m) + this.mu.RUnlock() return l } // 清空set func (this *StringSet) Clear() { - this.Lock() - this.M = make(map[string]struct{}) - this.Unlock() + this.mu.Lock() + this.m = make(map[string]struct{}) + this.mu.Unlock() } // 转换为数组 func (this *StringSet) Slice() []string { - this.RLock() - ret := make([]string, len(this.M)) + this.mu.RLock() + ret := make([]string, len(this.m)) i := 0 - for item := range this.M { + for item := range this.m { ret[i] = item i++ } - this.RUnlock() + this.mu.RUnlock() return ret } // 转换为字符串 func (this *StringSet) String() string { - s := this.Slice() - return fmt.Sprint(s) + return fmt.Sprint(this.Slice()) } diff --git a/g/os/groutine/groutine_api.go b/g/os/grpool/grpool_api.go similarity index 87% rename from g/os/groutine/groutine_api.go rename to g/os/grpool/grpool_api.go index 3dd84767a..950e54388 100644 --- a/g/os/groutine/groutine_api.go +++ b/g/os/grpool/grpool_api.go @@ -6,7 +6,7 @@ // Goroutine池. // 用于goroutine复用,提升异步操作执行效率. -package groutine +package grpool import ( "math" @@ -41,16 +41,21 @@ type PoolJob struct { var defaultPool = New(gDEFAULT_EXPIRE_TIME) // 创建goroutine池管理对象,给定过期时间(秒) -func New(expire int) *Pool { +// 第二个参数设置允许同时执行的最大任务数,用户可以限制最大并行任务数(非必需参数,默认为不限制) +func New(expire int, sizes...int) *Pool { + size := math.MaxUint32 + if len(sizes) > 0 { + size = sizes[0] + } p := &Pool { expire : int32(expire), queue : glist.NewSafeList(), funcs : glist.NewSafeList(), - funcEvents : make(chan struct{}, math.MaxUint32), + funcEvents : make(chan struct{}, size), stopEvents : make(chan struct{}, 1), } - p.workloop() - p.clearloop() + p.startWorkLoop() + p.startClearLoop() return p } diff --git a/g/os/groutine/groutine_job.go b/g/os/grpool/grpool_job.go similarity index 98% rename from g/os/groutine/groutine_job.go rename to g/os/grpool/grpool_job.go index 64031296e..939ba39b9 100644 --- a/g/os/groutine/groutine_job.go +++ b/g/os/grpool/grpool_job.go @@ -4,7 +4,7 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -package groutine +package grpool import "gitee.com/johng/gf/g/os/gtime" diff --git a/g/os/groutine/groutine_pool.go b/g/os/grpool/grpool_pool.go similarity index 96% rename from g/os/groutine/groutine_pool.go rename to g/os/grpool/grpool_pool.go index 7e8b6241a..d7ebc6313 100644 --- a/g/os/groutine/groutine_pool.go +++ b/g/os/grpool/grpool_pool.go @@ -4,7 +4,7 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. -package groutine +package grpool import ( "time" @@ -13,7 +13,7 @@ import ( ) // 任务分配循环 -func (p *Pool) workloop() { +func (p *Pool) startWorkLoop() { go func() { for { select { @@ -27,7 +27,7 @@ func (p *Pool) workloop() { } // 定时清理过期任务 -func (p *Pool) clearloop() { +func (p *Pool) startClearLoop() { go func() { for { time.Sleep(gDEFAULT_CLEAR_INTERVAL*time.Second) diff --git a/g/os/groutine/groutine_test.go b/g/os/grpool/grpool_test.go similarity index 85% rename from g/os/groutine/groutine_test.go rename to g/os/grpool/grpool_test.go index b89cf1835..ca11d8403 100644 --- a/g/os/groutine/groutine_test.go +++ b/g/os/grpool/grpool_test.go @@ -6,11 +6,11 @@ // go test *.go -bench=".*" -package groutine_test +package grpool_test import ( "testing" - "gitee.com/johng/gf/g/os/groutine" + "gitee.com/johng/gf/g/os/grpool" ) func increment() { @@ -19,7 +19,7 @@ func increment() { func BenchmarkGroutine(b *testing.B) { for i := 0; i < b.N; i++ { - groutine.Add(increment) + grpool.Add(increment) } } diff --git a/geg/os/groutine.go b/geg/os/groutine.go index 2a778aaa1..0df3fcec7 100644 --- a/geg/os/groutine.go +++ b/geg/os/groutine.go @@ -2,9 +2,9 @@ package main import ( "time" - "gitee.com/johng/gf/g/os/groutine" "fmt" "gitee.com/johng/gf/g/os/gtime" + "gitee.com/johng/gf/g/os/grpool" ) func job(i int) { @@ -14,12 +14,12 @@ func job(i int) { func main() { for i := 0; i < 10; i++ { - groutine.Add(func() { + grpool.Add(func() { job(i) }) } gtime.SetInterval(2*time.Second, func() bool { - fmt.Println(groutine.Size()) + fmt.Println(grpool.Size()) return true }) time.Sleep(5000*time.Second)