1
似乎有各種各樣的使用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
任何見解我在做什麼錯?
完美,我不知道.Handler。謝謝! – Carter 2014-11-23 07:56:16