```go
func middleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Executing middleware before request phase!")
// Pass control back to the handler
//handler.ServeHTTP(w, r)
fmt.Println("Executing middleware after response phase!")
})
}
```
看到上面的代码有一个疑问
HandlerFunc 函数接收两个参数调,上面的代码调用HandlerFunc 函数传递的是一个匿名函数。HandlerFunc 函数声明源码如下:
type HandlerFunc func(ResponseWriter, *Request)
有疑问加站长微信联系(非本文作者)