awesome-patterns/creational/object_pool.go
2019-10-31 02:07:57 +08:00

28 lines
297 B
Go

package pool
type Pool chan *Object
func New(total int) *Pool {
p := make(Pool, total)
for i := 0; i < total; i++ {
p <- new(Object)
}
return &p
}
p := New(2)
select {
case obj := <-p:
obj.Do( /*...*/ )
p <- obj
default:
// No more objects left — retry later or fail
return
}