Go语言通过高阶函数、接口组合和函数类型实现装饰器模式,核心是定义统一函数签名如type HandlerFunc func(ctx context.Context, req interface{}) (interface{}, error),以此支持链式调用;典型示例如WithLogging与WithTimeout装饰器分别添加日志和超时控制,形成handler := WithLogging(WithTimeout(5 * time.Second)(myBusinessHandler))的嵌套结构;当逻辑复杂时可用结构体封装如Retrier实现重试机制,提升配置灵活性;此外通过接口嵌入(如LoggingService嵌入Service)可实现面向对象风格的运行时装饰,保持接口兼容的同时增强行为;最终选择函数链、结构体或接口委托取决于扩展性、清晰度与测试便利性。

Go 语言没有原生的装饰器语法(如 Python 的 @decorator),但可以通过高阶函数、接口组合和函数类型来优雅地实现装饰器模式的核心思想:在不修改原始逻辑的前提下,动态增强行为(如日志、重试、超时、熔断等)。
核心是把业务逻辑抽象为统一的函数签名,例如:
type HandlerFunc func(ctx context.Context, req interface{}) (interface{}, error)
所有中间件或装饰器都接收并返回这个类型,形成链式调用:
HandlerFunc 并返回新 HandlerFunc 的函数以一个带日志和上下文超时的装饰器为例:
立即学习“go语言免费学习笔记(深入)”;
func WithLogging(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, req interface{}) (interface{}, error) {
log.Printf("→ Start: %T", req)
defer log.Printf("← Done")
return next(ctx, req)
}
}
<p>func WithTimeout(d time.Duration) func(HandlerFunc) HandlerFunc {
return func(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, req interface{}) (interface{}, error) {
ctx, cancel := context.WithTimeout(ctx, d)
defer cancel()
return next(ctx, req)
}
}
}
使用时链式组合:
handler := WithLogging(WithTimeout(5 * time.Second)(myBusinessHandler))
当装饰逻辑变复杂(如需配置、状态、多个选项),推荐用结构体封装:
type RetryConfig struct {
MaxAttempts int
Backoff time.Duration
}
<p>type Retrier struct {
cfg RetryConfig
}</p><p>func (r <em>Retrier) Wrap(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var err error
var resp interface{}
for i := 0; i <= r.cfg.MaxAttempts; i++ {
resp, err = next(ctx, req)
if err == nil || !shouldRetry(err) {
break
}
if i < r.cfg.MaxAttempts {
time.Sleep(r.cfg.Backoff </em> time.Duration(1<<i)) // 指数退避
}
}
return resp, err
}
}
这样调用更清晰:
retrier := &Retrier{cfg: RetryConfig{MaxAttempts: 3, Backoff: time.Second}}
handler := retrier.Wrap(WithLogging(myHandler))
若已有接口定义(如 Service),可通过组合+匿名字段实现“透明装饰”:
type Service interface {
Do(ctx context.Context, input string) (string, error)
}
<p>type LoggingService struct {
Service // 嵌入原始服务
}</p><p>func (l *LoggingService) Do(ctx context.Context, input string) (string, error) {
log.Printf("LoggingService.Do called with: %s", input)
return l.Service.Do(ctx, input) // 委托给原始实现
}</p><p>// 使用:&LoggingService{Service: realService}
这种写法适合需要保持接口兼容性、且装饰逻辑较固定的场景。
基本上就这些。Go 的装饰器不是语法糖,而是基于组合与函数式思维的实践模式——重点不在“像不像”,而在“好不好扩展、清不清楚职责、方不方便测试”。合理选用函数链 or 结构体封装 or 接口委托,就能写出既简洁又健壮的增强逻辑。
以上就是如何在Golang中实现装饰器模式_Golang装饰器模式功能增强技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号