Is there a way to get the current route that triggered an http.HandleFunc
? Maybe something like this?
http.HandleFunc("/foo/", serveFoo)
func serveFoo(rw http.ResponseWriter, req *http.Request) {
fmt.Println(http.CurrentRoute())
// Should print "/foo/"
}
The reason I want to get the current route is because I find myself writing code like this often.
if req.URL.Path != "/some-route/" {
http.NotFound(resp, req)
return
}
// or
key := req.URL.Path[len("/some-other-route/"):]
It would be nice if the code was a bit more copy-pastable, modular, and DRY like this.
if req.URL.Path != http.CurrentRoute() {
http.NotFound(resp, req)
return
}
// or
key := req.URL.Path[http.CurrentRoute():]
This is really just a small thing, so I'd rather not bring a whole other dependency into my project (Gorilla Mux).