O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The sync package contains the struct for pooling the resources. The Pool struct has the Get and Put method to retrieve and put the resource back to the pool. The Pool struct is considered to be safe for concurrent access.

While creating the Pool struct, the New field needs to be set. The New field is a no-argument function that should return the pointer to the pooled item. This function is then called in case the new object in the pool needs to be initialized.

Note from the logs of the preceding example, that the Worker is reused while returned to the pool. The important fact is that there shouldn't be any assumption related to the retrieved items by Get and returned items to Put method (like I've put three objects to pool ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required