In go, how do I use a closure with a gorilla/mux subrouter?

751 views Asked by At

There seem to be all sorts of examples of using a HandlerFunc closure similar to this one: http://codegangsta.gitbooks.io/building-web-apps-with-go/content/controllers/README.html

However I can't get it to work with a subrouter. Example:

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)
}

Gives me this error:

cannot use MyHandler(renderer) (type http.Handler) as type func(http.ResponseWriter, *http.Request) in function argument

Any insights into what I'm doing wrong?

1

There are 1 answers

1
James Henstridge On BEST ANSWER

The HandlerFunc method on Route expects to be passed a function, as the error message indicates. If instead you have an http.Handler, call Handler instead:

subroutes.Methods("GET").Handler(MyHandler(renderer))

Or alternatively, have your MyHandler function return the handler function directly rather than wrapping it as an http.Handler. Which option you choose is going to be a matter of style, and depend on the rest of your program.