Go语言通过高阶函数实现装饰器模式,可在不修改原函数的情况下动态增强功能。例如用WithLogging记录日志、WithTiming测量耗时,二者可组合使用,顺序影响执行流程;在HTTP服务中,LoggingMiddleware和TimingMiddleware可作为中间件嵌套到Handler链中,实现请求日志与性能监控。自Go 1.18支持泛型后,装饰器模式更通用,但现有方法已满足多数场景需求。

Go语言虽然没有像Python那样的原生装饰器语法,但通过函数式编程和接口设计,可以实现类似Decorator(装饰器)的模式。这种模式允许我们在不修改原始函数或结构的前提下,动态增强其功能。常见应用场景包括日志记录、权限校验、性能监控等。
装饰器模式是一种结构型设计模式,它允许你动态地给对象或函数添加行为,而不会影响其他对象。在Go中,我们通常使用高阶函数来实现这一思想——即函数接收另一个函数作为参数,并返回一个新的函数。
例如,有一个处理请求的函数:func handleRequest(name string) string {
return "Hello, " + name
}
如果我们想在调用前后打印日志,可以通过装饰器包装它。
定义一个装饰器函数,用于记录函数的输入和执行时间:
立即学习“go语言免费学习笔记(深入)”;
func WithLogging(fn func(string) string) func(string) string {
return func(name string) string {
fmt.Printf("Calling function with: %s\n", name)
defer fmt.Println("Function call completed.")
return fn(name)
}
}
使用方式如下:
decorated := WithLogging(handleRequest)
result := decorated("Alice")
fmt.Println(result)
输出为:
Calling function with: Alice Function call completed. Hello, Alice
这样就在不改动原函数逻辑的基础上增强了它的行为。
另一个常见用途是测量函数执行耗时:
func WithTiming(fn func(string) string) func(string) string {
return func(name string) string {
start := time.Now()
result := fn(name)
fmt.Printf("Execution time: %v\n", time.Since(start))
return result
}
}
组合多个装饰器也很容易:
finalHandler := WithLogging(WithTiming(handleRequest))
finalHandler("Bob")
注意装饰顺序会影响输出顺序,外层装饰器先执行。
在Web服务中,装饰器非常适用于中间件逻辑。比如增强一个http.HandlerFunc:
func LoggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("[%s] %s\n", r.Method, r.URL.Path)
next(w, r)
}
}
func TimingMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next(w, r)
fmt.Printf("Request to %s took %v\n", r.URL.Path, time.Since(start))
}
}
在路由中使用:
http.HandleFunc("/hello", LoggingMiddleware(TimingMiddleware(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
})))
也可以写成更清晰的链式调用形式:
handler := LoggingMiddleware(
TimingMiddleware(
helloHandler,
),
)
以上就是Golang Decorator功能增强与装饰示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号