1 回答
TA贡献1876条经验 获得超7个赞
alice.Constructor具有相同的签名但它被定义为另一种类型。所以你不能只使用它。
观看此https://www.youtube.com/watch?v=Vg603e9C-Vg 它有一个很好的解释。
你可以做的是这样使用type aliases :
var Middleware = alice.Constructor
看起来像这样:
前:
func timeoutHandler(h http.Handler) http.Handler {
return http.TimeoutHandler(h, 1*time.Second, "timed out")
}
func myApp(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world!"))
}
type Middleware func(http.Handler) http.Handler
func main() {
middlewares := []Middleware{timeoutHandler}
http.Handle("/", alice.New(middlewares...).ThenFunc(myApp))
}
后:
func timeoutHandler(h http.Handler) http.Handler {
return http.TimeoutHandler(h, 1*time.Second, "timed out")
}
func myApp(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world!"))
}
type Middleware = alice.Constructor
func main() {
middlewares := []Middleware{timeoutHandler}
http.Handle("/", alice.New(middlewares...).ThenFunc(myApp))
}
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报