企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
对象池 === ### 使用buffered channel实现对象池 ![](https://box.kancloud.cn/03ed2deefeec0398e034e4d7b97d2e8f_895x179.png) ~~~ type TargetObj struct { } type ObjPoll struct { poll chan *TargetObj } func NewObjPoll(num int) *ObjPoll { poll := &ObjPoll{ poll: make(chan *TargetObj, num), } for i := 0; i < num; i++ { poll.poll <- &TargetObj{} } return poll } func (o *ObjPoll) Get(timeout time.Duration) (*TargetObj, error) { select { case data := <-o.poll: return data, nil case <-time.After(timeout): return nil, errors.New("time out") } } func (o *ObjPoll) Pul(obj *TargetObj) error { select { case o.poll <- obj: return nil default: return errors.New("overflow") } } ~~~