Repository navigation
😀 小更新
📝 本次更新
- 封装链式调用的 Docker
- 增加两个设计模式的用例
- Observer 模式
- Option 模式
🖥 Use cases
-
Dockerfunc main(){ // 使用函数式配置构建 AutoTask 结构体 at := NewAutoTask(Option{}). WithImage("mysql:latest"). WithContainerName("api-mysql"). // 自定义网络 WithNetwork("YouNetWork"). WithStaticIp("172.20.0.10"). WithSubnet("172.20.0.0/16", "172.20.0.1"). WithEnv([]string{ "MYSQL_ROOT_PASSWORD=root", "MYSQL_PORT=3306", }, ). WithPortMappings(map[string]string{ "3306/tcp": "3306", }) if at.ContainerExists(at.opt.ContainerName) { // 运行 AutoTask at.Run() } }
-
Observer
// ConcreteObserver 具体观察者实现 type ConcreteObserver struct { name string } // Update 更新观察者状态 func (o *ConcreteObserver) Update(subject interface{}) { fmt.Printf("%s received update from subject\n", o.name) } func main() { // 创建具体主题 subject := &ConcreteSubject{} // 创建观察者池 pool, _ := ants.NewPool(10) // 向主题注册观察者 for i := 1; i <= 5; i++ { observer := &ConcreteObserver{name: fmt.Sprintf("Observer %d", i)} subject.RegisterObserver(observer) // 使用观察者池并发执行观察者更新操作 pool.Submit(func() { observer.Update(subject) }) } // 通知所有观察者 subject.NotifyObservers() // 关闭观察者池 pool.Release() }