Skip to content

Instantly share code, notes, and snippets.

@stvoidit
Created August 10, 2022 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stvoidit/48bf80336e934234d1afb9477cf557db to your computer and use it in GitHub Desktop.
Save stvoidit/48bf80336e934234d1afb9477cf557db to your computer and use it in GitHub Desktop.
sync.Pool -> bytes.Buffer
func NewBufferPool(bufsize int) BufferPool {
if bufsize <= 0 {
bufsize = bytes.MinRead
}
return BufferPool{pool: &sync.Pool{New: func() any {
return bytes.NewBuffer(make([]byte, 0, bufsize))
}}}
}
type BufferPool struct {
pool *sync.Pool
}
func (bp BufferPool) Get() *bytes.Buffer {
return bp.pool.Get().(*bytes.Buffer)
}
func (bp BufferPool) Put(b *bytes.Buffer) {
b.Reset()
bp.pool.Put(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment