如何在Golang中实现装饰器模式_Golang装饰器模式功能增强技巧

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 的函数
  • 原始处理器作为参数传入,装饰器在其前后插入逻辑
  • 支持无限嵌套,顺序即执行顺序

实现典型装饰器:日志 + 超时

以一个带日志和上下文超时的装饰器为例:

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)
    }
}

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
}

type Retrier struct { cfg RetryConfig }

func (r 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 time.Duration(1<

这样调用更清晰:

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)
}

type LoggingService struct { Service // 嵌入原始服务 }

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) // 委托给原始实现 }

// 使用:&LoggingService{Service: realService}

这种写法适合需要保持接口兼容性、且装饰逻辑较固定的场景。

基本上就这些。Go 的装饰器不是语法糖,而是基于组合与函数式思维的实践模式——重点不在“像不像”,而在“好不好扩展、清不清楚职责、方不方便测试”。合理选用函数链 or 结构体封装 or 接口委托,就能写出既简洁又健壮的增强逻辑。