Created
August 10, 2022 10:30
-
-
Save stvoidit/48bf80336e934234d1afb9477cf557db to your computer and use it in GitHub Desktop.
sync.Pool -> bytes.Buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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