Go中策略模式用函数类型+接口+组合实现,func比单方法接口更轻量,context承载元信息,map注册策略最安全高效。
Go 语言没有类和继承,但策略模式依然可以清晰实现——关键不是模仿其他语言的结构,而是用函数类型、接口和组合把“算法可替换”这个核心逻辑落地。
func 类型定义策略比接口更轻量当策略行为简单(比如只有一两个参数、返回单一值),直接用函数类型比定义接口更直观、更少冗余。Go 标准库中 sort.Slice 就是典型例子:它接受一个 func(int, int) bool 作为比较策略。
type Comparator interface { Compare(a, b interface{}) bool } 只会增加类型转换开销func(ctx context.Context, req *http.Request) (*http.Response, error)
Context 不只是传取消信号,更是策略执行的元信息载体别只把 context.Context 当作 cancel/timeout 工具。在策略模式中,它可承载策略决策所需的运行时上下文:用户角色、请求来源、灰度标识等。这些信息不进策略接口参数,而通过 context.WithValue 注入,让策略实现保持无状态。
func(ctx context.Context, input interface{}) (output interface{}, err error),便于统一调度if userID == "admin" {...}),改为从 ctx.Value(key) 动态提取context.WithValue 的 key 类型必须是 unexported 类型,防止冲突;建议用私有 struct 或 type ctxKey struct{}
map[str
ing]StrategyFunc 而非反射动态选择策略最常用也最安全的方式是字符串映射。Go 的反射成本高、可读性差、IDE 支持弱,而 map 查找快、调试直观、编译期可验证。
type StrategyFunc func(context.Context, interface{}) (interface{}, error)
var strategies = map[string]StrategyFunc{
"payment_alipay": handleAlipay,
"payment_wechat": handleWechat,
"payment_unionpay": handleUnionPay,
}
func GetStrategy(name string) (StrategyFunc, error) {
if fn, ok := strategies[name]; ok {
return fn, nil
}
return nil, fmt.Errorf("unknown strategy: %s", name)
}
nil 检查,避免运行时 panicinterface{} 做策略参数,优先定义具体输入结构体,提升类型安全和文档性真正难的不是写几个策略函数,而是让策略之间共享状态时不互相污染、让新策略上线不改老代码、让错误策略不影响主流程——这些靠的是 context 生命周期管理、策略执行 wrapper 和 fallback 机制,而不是接口设计本身。