I want to extend the http.Request
structure to include arguments present in the url. I know there are excellent libraries like gorilla that already do this, but I want to learn how its done on the nuts and bolts level.
Here's an example of what I'm trying to do:
// server.go
func Vars(r *http.Request) map[string]string {
var args map[string]string
args = make(map[string]string)
s := r.URL.Path.split('/')[1:]
for str := range s {
_, arg, _ := regexp.MatchString("\{\w+\}", str)
append(args, arg)
}
return args
}
httpMock.HandleFunc("/myurl/{arg1}/", MyHandler)
func MyHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Success! " + r.Vars()["arg1"]))
}
But I get an index out of range exception. Can anyone help me figure out how to do this?