/go-pools

A collection of some pools, such as BufferPool, BytesPool and TaskPool.

Primary LanguageGoApache License 2.0Apache-2.0

go-pools Build Status GoDoc License

Provide an object pool based on the generics supporting Go1.18+, such as Pool, CapPool.

Install

$ go get -u github.com/xgfone/go-pools

Example

Pool

type Context struct {
    // ....
}
pool := New(func() *Context { return new(Context) })

// Get the context from the pool.
ctx := pool.Get()

// Use the object as *Context to do something.
fmt.Println(ctx.Object) // ctx.Object => *Context
// ...

// Release the context into the pool.
ctx.Release()

// Output:
// &{}

CapPool

// For *bytes.Buffer
bufferPool := NewCapPool(
    func(cap int) *bytes.Buffer { return bytes.NewBuffer(make([]byte, 0, cap)) }, // new
    func(buf *bytes.Buffer) *bytes.Buffer { buf.Reset(); return buf },            // reset
)

buffer := bufferPool.Get(1024)
// TODO ...
buffer.Release()

// For a slice, such as []byte or []interface{}
slicePool := NewCapPool(
    func(cap int) []byte { return make([]byte, 0, cap) }, // new
    func(buf []byte) []byte { return buf[:0] },           // reset
)

bytes := slicePool.Get(128)
// TODO ...
bytes.Release()