bufpool

package
v0.0.0-...-8dcffa4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2019 License: Apache-2.0 Imports: 2 Imported by: 15

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get() *bytes.Buffer

Get returns a buffer from the pool.

func Put

func Put(buf *bytes.Buffer)

Put returns a buffer to the pool. The buffer is reset before it is put back into circulation.

Types

type Tank

type Tank struct {
	// contains filtered or unexported fields
}

Tank implements a sync.Pool for bytes.Buffer

func New

func New(size int) Tank

New instantiates a new bytes.Buffer pool with a custom pre-allocated buffer size.

func (Tank) Get

func (t Tank) Get() *bytes.Buffer

Get returns type safe a buffer

func (Tank) Put

func (t Tank) Put(buf *bytes.Buffer)

Put empties the buffer and returns it back to the pool.

bp := New(320)
buf := bp.Get()
defer bp.Put(buf)
// your code
return buf.String()

If you use Bytes() function to return bytes make sure you copy the data away otherwise your returned byte slice will be empty. For using String() no copying is required.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL