You can create a handler for this outside of the http package, which
is (IMO) where it belongs. I'm not even convinced that it belongs in
the core library, as there are many ways you can do URL routing and
none of them are standard. It's the kind of thing that there would be
lots of disagreement and bikeshedding about.
Here's a simple way to route with regexps that passes the parameters
as a slice of strings:
(warning: uncompiled/untested code lies ahead)
type route struct {
re *regexp.Regexp
handler func(http.ResponseWriter, *http.Request, []string)
}
type RegexpHandler struct {
routes []*route
}
func (h *RegexpHandler) AddRoute(re string, handler
func(http.ResponseWriter, *http.Request, []string)) {
r := &route{regexp.MustCompile(re), handler}
h.routes = append(h.routes, r)
}
func (h *RegexpHandler) ServeHTTP(rw http.ResponseWriter, r *Request) {
for _, route := range h.routes {
matches := route.re.FindStringSubmatch(r.RawURL)
if matches != nil {
route.handler(rw, r, matches)
break
}
}
}
For your case, you can do something like:
reHandler := new(RegexpHandler)
reHandler.AddRoute("/people/[0-9]+/edit$", peopleEditHandler)
http.ListenAndServe(":8080", reHandler)
- Evan
Of course, as Evan said, there's nothing stopping you from
writing your own regexp-based mux and registering it to
handle "/". That's part of the beauty of interfaces.
Russ
if you continue to allow the current Go approach
of allowing separate packages to register their
own http handlers (for example expvar), then this
can't work, as init functions are called in arbitrary
order.
indeed, and this is something we want to keep.
if you don't want to use the default handlers,
don't use the default servemux.
russ