似乎有各种各样的使用类似于这个的 HandlerFunc 闭包的例子:http ://codegangsta.gitbooks.io/building-web-apps-with-go/content/controllers/README.html
但是我不能让它与子路由器一起工作。例子:
func MyHandler(renderer *render.Render) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
renderer.HTML(rw, http.StatusOK, "subroute/index", nil)
})
}
func main() {
renderer := render.New(render.Options{Layout: "base"})
router := mux.NewRouter().StrictSlash(false)
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
...
})
subroutes := router.Path("/subroute").Subrouter()
subroutes.Methods("GET").HandlerFunc(MyHandler(renderer))
http.Handle("/", router)
log.Println("Listening...")
http.ListenAndServe(":3000", nil)
}
给我这个错误:
cannot use MyHandler(renderer) (type http.Handler) as type func(http.ResponseWriter, *http.Request) in function argument
对我做错了什么有任何见解吗?
蝴蝶刀刀
相关分类