mirror of
https://github.com/datarhei/core.git
synced 2025-10-05 16:07:07 +08:00
34 lines
409 B
Go
34 lines
409 B
Go
package mem
|
|
|
|
import (
|
|
"bytes"
|
|
"sync"
|
|
)
|
|
|
|
type BufferPool struct {
|
|
pool sync.Pool
|
|
}
|
|
|
|
func NewBufferPool() *BufferPool {
|
|
p := &BufferPool{
|
|
pool: sync.Pool{
|
|
New: func() any {
|
|
return &bytes.Buffer{}
|
|
},
|
|
},
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
func (p *BufferPool) Get() *bytes.Buffer {
|
|
buf := p.pool.Get().(*bytes.Buffer)
|
|
buf.Reset()
|
|
|
|
return buf
|
|
}
|
|
|
|
func (p *BufferPool) Put(buf *bytes.Buffer) {
|
|
p.pool.Put(buf)
|
|
}
|