Go语言通过高阶函数和闭包实现装饰器模式,可在不修改原函数的前提下为其添加日志、权限校验、超时控制等功能。1. 使用func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc为HTTP处理函数添加日志;2. 通过链式调用组合多个装饰器,如loggingMiddleware(authMiddleware(timeoutMiddleware(handler))));3. 可扩展至普通函数,如timeIt统计执行时间。关键在于函数作为一等公民传递,结合闭包封装逻辑,注意上下文传递与错误处理。

在Go语言中,虽然没有像Python那样的装饰器语法糖,但可以通过函数式编程和高阶函数的方式实现装饰器模式。装饰器模式的核心是在不修改原始函数逻辑的前提下,为其增加额外功能,比如日志记录、权限校验、耗时统计等。
使用高阶函数实现装饰器
Go中的函数是一等公民,可以作为参数传递或返回值。利用这一点,我们可以定义一个返回函数的函数,即“装饰器”。
例如,为一个HTTP处理函数添加日志功能:
func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("Received request: %s %s", r.Method, r.URL.Path)
next(w, r)
log.Printf("Completed request: %s %s", r.Method, r.URL.Path)
}
}使用方式:
立即学习“go语言免费学习笔记(深入)”;
http.HandleFunc("/hello", loggingMiddleware(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}))链式装饰器
多个装饰器可以层层嵌套,形成调用链。例如,添加超时控制和身份验证:
dmSOBC SHOP网店系统由北京时代胜腾信息技术有限公司(http://www.webzhan.com)历时6个月开发完成,本着简单实用的理念,商城在功能上摒弃了外在装饰的一些辅助功能,尽可能的精简各项模块开发,做到有用的才开发,网店V1.0.0版本开发完成后得到了很多用户的使用并获得了好评,公司立即对网店进行升级,其中包括修正客户提出的一些意见和建议,现对广大用户提供免费试用版本,如您在使用
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next(w, r)
}
}
func timeoutMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 5time.Second)
defer cancel()
done := make(chan bool, 1)
go func() {
next(w, r.WithContext(ctx))
done <- true
}()
select {
case <-done:
case <-ctx.Done():
http.Error(w, "Request timeout", http.StatusGatewayTimeout)
}
}}
组合使用:
handler := loggingMiddleware(authMiddleware(timeoutMiddleware(helloHandler)))
http.HandleFunc("/hello", handler)通用函数装饰器(适用于普通函数)
不仅限于HTTP处理函数,也可以为普通函数写装饰器。比如统计函数执行时间:
func timeIt(fn func(int) int) func(int) int {
return func(n int) int {
start := time.Now()
result := fn(n)
log.Printf("Function took %v\n", time.Since(start))
return result
}
}使用示例:
slowFunc := timeIt(func(n int) int {
time.Sleep(2 * time.Second)
return n * 2
})
slowFunc(5) // 输出耗时信息
基本上就这些。Go通过高阶函数和闭包天然支持装饰器模式,关键是理解函数类型匹配和中间逻辑的封装方式。不复杂但容易忽略细节,比如上下文传递和错误处理。









