I am using net/http http.HandleFunc("/resource", resource.Handle) and I was wondering if there was a way to see what route (in this case /resource) is used to get you to resource.Handle? Or do I have to create a Mux for this?
I'd like to know this to extract the resource from the url path to do some magic with it...
Yes you can
The main points to do:
DefaultServeMux
used by the HandleFunc
method.http.Request
For Example:
package main
import (
"fmt"
"net/http"
"net/url"
)
func main() {
theUrl, err := url.Parse("/response")
if err != nil {
fmt.Println(err.Error())
return
}
http.HandleFunc("/response", func(w http.ResponseWriter, r *http.Request) {
})
handler, path := http.DefaultServeMux.Handler(&http.Request{Method: "GET", URL: theUrl})
fmt.Println(handler, path)
}
see this Go Playground
http://golang.org/pkg/net/http/#Request
Use request.URL.Path
to get the path used to access the handler.