I would like to get the path for a named subroute but my code below does not work. When I try to use the same logic on a non-subroute, it works fine. How do I get the path of a named subroute?
router = mux.NewRouter() // this is a global variable
home := router.Path("/home").Subrouter()
home.Methods("GET").HandlerFunc(c.GetHomeHandler).Name("home")
home.Methods("POST").HandlerFunc(c.PostHomeHandler)
p, err := router.Get("home").URL()
if (err != nil) { panic (err) }
log.Printf(p.Path)
The above gives this error:
panic: mux: route doesn't have a host or path
Now if I do router.HandleFunc("/home", c.GetHomeHandler).Name("home")
, it works just fine.
Appreciate your help.
Update:
This is a reasonable workaround but it avoids creating a Subroute. It is fine for the example I had above but probably not ideal as you will not get all the benefits of a Subroute.
router.Path("/home").Methods("GET").HandlerFunc(c.GetHomeHandler).Name("home")
router.Path("/home").Methods("POST").HandlerFunc(c.PostHomeHandler)
Thanks!