GoCommonsPool是用Go实现的对象池,直接翻译自Java版的ApacheCommonsPool.
示例代码:
//use create funcpool := NewObjectPoolWithDefaultConfig(NewPooledObjectFactorySimple( func() (interface{}, error) { return &MyPoolObject{}, nil }))obj, _ := pool.BorrowObject()pool.ReturnObject(obj)//use custom Object factorytype MyObjectFactory struct {}func (this *MyObjectFactory) MakeObject() (*PooledObject, error) { return NewPooledObject(&MyPoolObject{}), nil}func (this *MyObjectFactory) DestroyObject(object *PooledObject) error { //do destroy return nil}func (this *MyObjectFactory) ValidateObject(object *PooledObject) bool { //do validate return true}func (this *MyObjectFactory) ActivateObject(object *PooledObject) error { //do activate return nil}func (this *MyObjectFactory) PassivateObject(object *PooledObject) error { //do passivate return nil}pool := NewObjectPoolWithDefaultConfig(new(MyObjectFactory))obj, _ := pool.BorrowObject()pool.ReturnObject(obj)
评论