1 Star 1 Fork 0

凡卡 / utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sync_list.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
凡卡 提交于 2022-09-23 14:41 . sync_list添加删除方法
package utils
import (
"sync"
)
type SyncList struct {
lock *sync.RWMutex
list []interface{}
}
func (this *SyncList) Add(obj interface{}) {
this.lock.Lock()
this.list = append(this.list, obj)
this.lock.Unlock()
}
func (this *SyncList) Get(index int) (n interface{}) {
this.lock.RLock()
n = this.list[index]
this.lock.RUnlock()
return
}
func (this *SyncList) Remove(index int) (ok bool) {
this.lock.Lock()
if len(this.list) < index+1 {
ok = false
this.lock.Unlock()
return
}
temp := this.list[:index]
this.list = append(temp, this.list[index+1:]...)
ok = true
this.lock.Unlock()
return
}
func (this *SyncList) GetAll() []interface{} {
this.lock.RLock()
l := make([]interface{}, len(this.list))
copy(l, this.list)
this.lock.RUnlock()
return l
}
//func (this *SyncList) Range(fn func(i int, v interface{}) bool) {
// this.lock.Lock()
// for i, one := range this.list {
// if !fn(i, one) {
// break
// }
// }
// this.lock.Unlock()
//}
//func (this *SyncList) Find() {
// for _,one := range this.list{
// if
// }
//}
func NewSyncList() *SyncList {
return &SyncList{
lock: new(sync.RWMutex),
list: make([]interface{}, 0),
}
}
Go
1
https://gitee.com/prestonTao/utils.git
git@gitee.com:prestonTao/utils.git
prestonTao
utils
utils
a26ca6af1029

搜索帮助