Nice article – it was interesting to me that gorilla/mux has “route reversal” (though I’ve never needed that personally). Note that chi also supports host routing: not the package itself, but with the go-chi/hostrouter middleware that’s part of the chi project (it works as stand-alone middleware too).
For an in-depth look at building your own routing using regexp and other types of custom matching, see my article Different approaches to HTTP routing in Go. I also compare to Axel Wagner’s “ShiftPath” technique, and some of the popular router libraries (chi, gorilla, pat).
Great article, but IMO http.ServeMux has a couple of other gotchas in addition to the ones mentioned, that make it more painful to use than it should be:
No way to match “/foo” and “/foo/” with a single Handle() call - handler on “/foo/” will not match “/foo”, and vice versa.
“/foo/” matches the entire sub-tree “/foo/…” - there is no way to match a single path component under a sub-tree.
Yes, these can be worked around, but it’s usually just more straightforward to use a better router.
it’s usually just more straightforward to use a better router.
You can duplicate Handle calls to solve those problems. I dunno. Most of my projects these days have like zero to five dependencies? I think that’s something worth gunning for; if there’s a way to stay within the stdlib, even with a bit of toil, seems like it’s worth doing.
This is nice, now i want something similar for warp,actix-web,gotham,rocket,tide,nickel,thruster and axum. Bonus points for including hyper.
I’ve really enjoyed using go-chi. My requirements are rather basic, just simple routing.
Simple: use chi unless you have a really good reason not to.
Nice article – it was interesting to me that
gorilla/muxhas “route reversal” (though I’ve never needed that personally). Note thatchialso supports host routing: not the package itself, but with thego-chi/hostroutermiddleware that’s part of the chi project (it works as stand-alone middleware too).For an in-depth look at building your own routing using regexp and other types of custom matching, see my article Different approaches to HTTP routing in Go. I also compare to Axel Wagner’s “ShiftPath” technique, and some of the popular router libraries (chi, gorilla, pat).
Great article, but IMO
http.ServeMuxhas a couple of other gotchas in addition to the ones mentioned, that make it more painful to use than it should be:Yes, these can be worked around, but it’s usually just more straightforward to use a better router.
You can duplicate Handle calls to solve those problems. I dunno. Most of my projects these days have like zero to five dependencies? I think that’s something worth gunning for; if there’s a way to stay within the stdlib, even with a bit of toil, seems like it’s worth doing.
These are all path/pattern-oriented routers. Are there any good resource-oriented routers in the style of Rails?
NGL, this nerd snipped me into writing my own router.