Skip to content

Instantly share code, notes, and snippets.

@0xc0d
Last active October 25, 2020 01:20
Show Gist options
  • Save 0xc0d/518acb5b88631956fe4eb572feccbaee to your computer and use it in GitHub Desktop.
Save 0xc0d/518acb5b88631956fe4eb572feccbaee to your computer and use it in GitHub Desktop.
sync.Pool example
// A dummy struct
type Person struct {
Name string
}
// Initializing pool
var personPool = sync.Pool{
// New optionally specifies a function to generate
// a value when Get would otherwise return nil.
New: func() interface{} { return new(Person) },
}
// Main function
func main() {
// Get hold of an instance
newPerson := personPool.Get().(*Person)
// Defer release function
// After that the same instance is
// reusable by another routine
defer personPool.Put(newPerson)
// Using the instance
newPerson.Name = "Jack"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment