Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I agree, I wouldn't have been against a required return, but the mitigation is relatively straightforward by using custom handlers like mentioned at the end, if needed.

I really like Go's default HTTP handlers because it offers that straightforward flexibility. Nothing rocket-sciencey.



The beauty is in the simplicity. `http.Handler` is all about what is needed to handle HTTP and nothing else. It doesn't intend to make things pretty, or safe, or anything really, except to express the minimum surface area required to interact with HTTP while abstracting the commonalities. Beyond that, as the article mentions, it's quite easy to abstract and implement your own sugar layer.


> the mitigation is relatively straightforward by using custom handlers like mentioned at the end, if needed.

Why not implement ServeHTTP on the custom handler though? That's not exactly difficult:

    type MyHandlerFunc func(w http.ResponseWriter, r *http.Request) error

    func (f MyHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
     err := f(w, r)
     if err != nil {
      http.Error(w, err.Error(), http.StatusInternalServerError)
     }
    }
Then you can just register it with

    http.Handle("/hello", MyHandlerFunc(someHandler))


Actually a bunch of router/web helper libraries do this in different ways which makes it a huge pain to compose middleware from different libraries.

Even Mux's middleware library is not consistent in how it does this for different middlewares so they have to be applied different ways or even have wrapper functions written


This. Middlewares typically expect to wrap an http.Handler so you lose all the middleware composition ergonomics by adding a return value to your custom handler signature


Here it just implements the standard library’s interface rather than use some sort of conversion closure adapter, there’s nothing special to it.


There are definitely edge cases here (like what if the handler already wrote a body and it was sent on the wire, etc), but this is absolutely the pattern to use, and one of my favorite parts of a type system that allows methods on function types.


Yes, or what if you want to upgrade the connection to a websocket?


Well yeah, I think it’s important to let you write bytes to the socket and close it when you want to, and if people are paranoid about making obvious, easy to spot bugs, they can use some abstraction to protect themselves.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: