Go语言中通过goroutine和channel实现观察者模式异步通知,利用Subject管理Observer并广播事件,Notify中启动goroutine异步调用Update方法,避免阻塞发布者;为控制并发,可使用带缓冲channel作为信号量限制最大协程数,防止资源耗尽,从而实现高效、解耦的事件通知机制。

在Go语言中实现观察者模式的异步通知,核心是利用goroutine和channel来解耦事件发布与订阅处理,避免阻塞发布者。下面是一个简洁、实用的实现方式。
主题负责管理订阅者并广播事件,观察者定义接收通知的方法。
type Event struct {
Data interface{}
立即学习“go语言免费学习笔记(深入)”;
}
type Observer interface {
Update(event Event)
}
type Subject struct {
observers []Observer
}
func (s *Subject) Register(o Observer) {
s.observers = append(s.observers, o)
}
func (s *Subject) Notify(event Event) {
for _, observer := range s.observers {
go observer.Update(event) // 异步执行
}
}
每个观察者可以独立处理事件,比如写日志、发消息等。
type EmailService struct{}
func (e *EmailService) Update(event Event) {
// 模拟耗时操作
time.Sleep(100 * time.Millisecond)
fmt.Printf("邮件服务收到事件: %v\n", event.Data)
}
type LogService struct{}
func (l *LogService) Update(event Event) {
fmt.Printf("日志服务记录事件: %v\n", event.Data)
}
若观察者处理任务较重,可在Notify中通过带缓冲channel限流,避免goroutine泛滥。
func (s *Subject) NotifyWithLimit(event Event, maxGoroutines int) {
sem := make(chan struct{}, maxGoroutines)
for _, observer := range s.observers {
sem
go func(o Observer) {
defer func() { <-sem }()
o.Update(event)
}(observer)
}
// 等待所有任务释放信号量(可选:用WaitGroup更精确)
for i := 0; i < cap(sem); i++ {
sem
}
}
启动主题,注册多个观察者,触发异步通知。
func main() {
subject := &Subject{}
subject.Register(&EmailService{})
subject.Register(&LogService{})
subject.Notify(Event{Data: "用户注册成功"})
time.Sleep(time.Second) // 等待异步执行完成
}
基本上就这些。用goroutine直接调用Update是最简单有效的异步方式,加上信号量能更好控制系统负载。这种设计既保持了松耦合,又具备良好的扩展性。
以上就是Golang如何实现观察者模式异步通知的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号