Files
demo/pool.go
2025-03-14 18:50:49 +00:00

27 lines
351 B
Go

package goproxy
import "sync"
// 泛型对象池
type pool[T any] struct {
pool sync.Pool
}
func newPool[T any](newFunc func() T) *pool[T] {
return &pool[T]{
pool: sync.Pool{
New: func() interface{} {
return newFunc()
},
},
}
}
func (p *pool[T]) Get() T {
return p.pool.Get().(T)
}
func (p *pool[T]) Put(x T) {
p.pool.Put(x)
}