Live data from Hacker News

Better HTTP server routing in Go 1.22

eli.thegreenplace.net

131–140 of 236 posts

Re: Better HTTP server routing in Go 1.22

#131
post #102

Earlier quoted context omitted.

Surely the changes to panic on conflicting routes being discussed upstream already prove they're willing to flex on that guarantee?

That’s not a breaking change.

How is "this api usage was allowed before and now panics" not a breaking change?

Re: Better HTTP server routing in Go 1.22

#132

I don't like this. Is there a reason for using a stringified method prefix? I'd prefer the type safety of verb-specific methods (i.e. mux.Get, mux.Post etc) than magic strings validated at run time. Additionally editors can autocomplete/intellisense methods.

I also don't prefer using strings, but to be fair, HTTP methods are just strings when the request is received. There is some beauty in that in matches the prefix of the first line of an HTTP packet

On the wire, everything is a string. Doesn't make it a good type to use!

Re: Better HTTP server routing in Go 1.22

#133

Earlier quoted context omitted.

I would say that unless specified differently this bit of the documentation[1] still applies (second paragraph): > Longer patterns take precedence over shorter ones, https://pkg.go.dev/net/http#ServeMux

No, it does not. The length of the pattern isn't important anymore, only whether it overlaps with other patterns.

That would be a breaking API change in my opinion. I bet you the length criterion will still apply for path patterns that don't include placeholders, even if not specifically mentioned in the proposal.

[edit] Heh, I guess you'd know better. :D The comparison between the hn username and github username resolved favourably in the end. :P

Re: Better HTTP server routing in Go 1.22

#134

It's a nice change for little experimental programs, but production servers need lots of functionality that third party routers offer, like request middleware, better error handling, etc. It's tedious to build these on top of the native router, so convenience will steer people to excellent packages like Gin, Echo, Fiber, Gorilla, Chi, etc.

Big fan of chi. It is simple, and just works. Also matches the http.Handler interface, so for testing and otherwise just makes life so easy (like using httptest new server - pass it the chi.Mux)

Re: Better HTTP server routing in Go 1.22

#135
post #77
post #16

Good one less external dependency. As a side note sqlc + postgres + templ (kindah jsx for go) + htmx + tailwinds has being extremely productive stack to develop in

How do you handle tailwind here? I really like the idea of adding tailwind but if it means using npm I think I'd rather just write my own css.

I'm not OP, but Tailwind ships a binary you can use (by bundling up a JS runtime): https://tailwindcss.com/blog/standalone-cli

Re: Better HTTP server routing in Go 1.22

#136
post #13

Forcing 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…

> Are there go-specific reasons for that? In general , the reason you use a compiled / typed language like golang at all (instead of, say, perl) is to "left shift" your bugs: A bug caught when you first spin up your application is better than a bug caught after a corner case acts up in the wild, and a bug caught when you compile is better than a bug caught when you first spin up your application. I recently ran a cro…

> "left shift" your bugs

Am I doing it right?

Re: Better HTTP server routing in Go 1.22

#137

Earlier quoted context omitted.

Go as a language is generally not sophisticated enough to do that sort of thing. A counter- and/or example of this is what Service Weaver does to try to bomb builds when generated files are older than their dependencies.

I'm having trouble thinking of a mainstream production language with a stronger type system that could make a compile error out of the string argument passed to an HTTP router; can you think of one? Or of a non-string-typing for routes that solves the same problem, again in something people ordinarily use to deploy to production?

Most languages with macro or templating should be able to define this behavior as a library. Rust and C++ come to mind, for example.

Re: Better HTTP server routing in Go 1.22

#138

Earlier quoted context omitted.

Go as a language is generally not sophisticated enough to do that sort of thing. A counter- and/or example of this is what Service Weaver does to try to bomb builds when generated files are older than their dependencies.

I'm having trouble thinking of a mainstream production language with a stronger type system that could make a compile error out of the string argument passed to an HTTP router; can you think of one? Or of a non-string-typing for routes that solves the same problem, again in something people ordinarily use to deploy to production?

I'm inclined to suggest the typescript could make compile errors of ambiguous routes, though I don't see any obvious reasons way without an explicitly referenced agreggate type for existing routes. So perhaps not if routes are initialised implicitly like this.

Template type strings with inference would also allow you to parse the strings in the type system.

I cannot imagine wanting to build my routes table implicitly through import graph rather than having a specific place to aggregate.

Re: Better HTTP server routing in Go 1.22

#139
post #13

Earlier quoted context omitted.

> Are there go-specific reasons for that? In general , the reason you use a compiled / typed language like golang at all (instead of, say, perl) is to "left shift" your bugs: A bug caught when you first spin up your application is better than a bug caught after a corner case acts up in the wild, and a bug caught when you compile is better than a bug caught when you first spin up your application. I recently ran a cro…

> "left shift" your bugs Am I doing it right?

ug

Re: Better HTTP server routing in Go 1.22

#140

Earlier quoted context omitted.

This seems like an issue caused by registration at a distance. I'm new to go and it is one of the things that felt most wrong. I'm used to a router where you define a big list of routes all in one place, maybe in some DSL. That's very easy to refactor, and doesn't have any ambiguity. If a library wants a route registered it puts a snippet in the docs for users to copy. Go in general seems to value clarity over magic…

Go's flag, log, image, and sql packages all work this way, so it's not just the HTTP router. I sort of see both sides. Having implicit registration makes it very easy to have different teams working on different packages and you just import the package and it registers itself. But it also makes the behavior of the resulting final binary hard to understand and based on implicit code instead of explicit. I personally t…

It doesn't feel to me like much of a gain over having said team expose a specific default self registration function. Then that act can be explicit rather than action at a distance on some dependency defined global state.

I think this pattern in the standard library is a mistake.

Post reply on HN