Better HTTP server routing in Go 1.22
eli.thegreenplace.net
Better HTTP server routing in Go 1.22
1–10 of 236 posts
Re: Better HTTP server routing in Go 1.22
#2Re: Better HTTP server routing in Go 1.22
#3You need to weirdly create a magic string to define your handler. Why not make it an actual argument, then using already existing constants is easier.
[1] https://github.com/golang/go/issues/61410#issuecomment-16580...
Re: Better HTTP server routing in Go 1.22
#4Will this finally end the eternal golang http router bikeshedding?
Re: Better HTTP server routing in Go 1.22
#5Are there go-specific reasons for that? The edge case of "you might register HTTP routes in a bunch of places and it's harder to find that if multiple routes match" seems like something that be worked around with tooling.
I've (ab)used the behavior of "first to be matched wins" a ton in my career - there's been a bunch of "business case" times when I've needed like `/foo/bar` to be one registered route, and `/foo/{id}` to be another.
Re: Better HTTP server routing in Go 1.22
#6In their example here, had they defined the `/task/0/{action}/` path before the wildcard path, my expectation would have been for that to match first. This would allow for handlers for special cases to easily be defined.
I’d far rather things just do what I say than fail in “helpful” ways. Smells really funny and not very Go.
Re: Better HTTP server routing in Go 1.22
#7Re: Better HTTP server routing in Go 1.22
#8As I commented[1] I think the syntax is flawed in the proposal. You need to weirdly create a magic string to define your handler. Why not make it an actual argument, then using already existing constants is easier. [1] https://github.com/golang/go/issues/61410#issuecomment-16580...
Adding new arguments to the Mux interface would break existing code, given the method signature can’t be changed, these magic strings seem like a reasonable compromise. It not like HTTP verbs are going to change anytime soon, and it’s trivial to validate them during register, or via static analysis. So I’m not sure what value the use of constants would bring, especially if it either broke backwards compatibility, or forced the creation of a new, but slightly different mux API that would have to live in parallel with the old API forever.
Re: Better HTTP server routing in Go 1.22
#9 router.GET("/", func(context *gin.Context) {
...
}
And it seems to be no different than many other languages/frameworks. Are there more examples that use the "GET /path/" way ?Re: Better HTTP server routing in Go 1.22
#10Forcing a panic when 2 routes are matched seems counter intuitive to me (versus, idk, every other web framework which uses the first-to-be-registered route that matches). Are there go-specific reasons for that? The edge case of "you might register HTTP routes in a bunch of places and it's harder to find that if multiple routes match" seems like something that be worked around with tooling. I've (ab)used the behavior…
Looks like it will panic in case you have `/foo/{id}/delete` and `/foo/bar/{action}`. /foo/bar/delete will match both, none is more specific so it panics. Feels reasonable. Having a first one wins precedence might be better though.