1 Star 0 Fork 0

iqingfeng/ngrok

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
ring.go 657 Bytes
Copy Edit Raw Blame History
package util
import (
"container/list"
"sync"
)
type Ring struct {
sync.Mutex
*list.List
capacity int
}
func NewRing(capacity int) *Ring {
return &Ring{capacity: capacity, List: list.New()}
}
func (r *Ring) Add(item interface{}) interface{} {
r.Lock()
defer r.Unlock()
// add new item
r.PushFront(item)
// remove old item if at capacity
var old interface{}
if r.Len() >= r.capacity {
old = r.Remove(r.Back())
}
return old
}
func (r *Ring) Slice() []interface{} {
r.Lock()
defer r.Unlock()
i := 0
items := make([]interface{}, r.Len())
for e := r.Front(); e != nil; e = e.Next() {
items[i] = e.Value
i++
}
return items
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/iqingfeng/ngrok.git
git@gitee.com:iqingfeng/ngrok.git
iqingfeng
ngrok
ngrok
e5a7b820743f

Search