Documentation ¶
Overview ¶
Pooler is a tool to automate the creation of typed sync.Pool wrappers. Given the name of a type T, pooler will create a new self-contained Go source file implementing
type TPool struct { sync.Pool } func NewTPool() *TPool func (*TPool) Get() *T func (*TPool) Put(*T)
The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.
For example, given this snippet,
package painkiller type Pill struct { i int }
running this command
pooler -type=Pill
in the same directory will create the file pill_pool.go, in package painkiller, containing a definition of
type PillPool struct { sync.Pool } func NewPillPool() *PillPool func (*PillPool) Get() *Pill func (*PillPool) Put(*Pill)
So now running
pp := NewPillPool() p := pp.Get() p.i = 2 pp.Put(p) p2 := pp.Get() // p2 & p points to same var
Will result in only one memory allocation of a Pill.
Typically this process would be run using go generate, like this:
//go:generate pooler -type=Pill
With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.
The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_pool.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.
This code is a small update from https://godoc.org/golang.org/x/tools/cmd/stringer .