我正在尝试按照 Go 博客:错误处理和 Go 中的appHandler描述实现一个。我有,现在我只是想把它连接到我的路线上。以下作品:appHandler
router := new(mux.Router)
router.Handle("/my/route/", handlers.AppHandler(handlers.MyRoute))
但是,我希望能够允许 GET 请求以及“StrictSlash(true)”。我有:
type Routes []Route
type Route struct {
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
var routes = Routes{
Route{"GET", "/my/route/", handlers.AppHandler(handlers.MyRoute)}
}
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
router.Methods(route.Method).Path(route.Pattern).Handler(handler)
}
AppHandler 看起来像:
type AppHandler func(http.ResponseWriter, *http.Request) *appError
func (fn AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// blah, blah, blah
}
我收到一个错误:
cannot use handlers.AppHandler(handlers.MyRoute) (type handlers.AppHandler) as type http.HandlerFunc in field value