From the course: Deep Dive Into Go Lang Interfaces
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Solution: Wrapping sync.Pool
From the course: Deep Dive Into Go Lang Interfaces
Solution: Wrapping sync.Pool
(upbeat music) - [Instructor] Let's have a look at my solution. So the struct, Pool is wrapping sync.Pool. So there is a pool field inside my struct. The New function is getting a newFn that is creating new objects in the pool if the pool is empty. So I'm creating an empty pool. And then if newFn is not nil, meaning there is a newFn, the pool newFn is a function without arguments that returns any. The newFn that we got is a function that gets no arguments, but return T, the current type for the pool, so I wrap it in anonymous function. And then finally, returning a pointer too. The Put is very simple, just call the underlying pool, Put. The Get is a bit more complex. So I'm getting, and what I'm getting back from the underlying pool is any. And if it's nil, means there's nothing in the pool. I'm creating a zero value and returning false saying there's nothing in the pool. Otherwise, I'm using a type assertion to convert it to the type that should be returned. And true, meaning, I…