Last active
October 25, 2020 01:20
-
-
Save 0xc0d/518acb5b88631956fe4eb572feccbaee to your computer and use it in GitHub Desktop.
sync.Pool example
This file contains 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
// 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