Hacker News new | ask | show | jobs
by elithrar 4420 days ago
> Gorilla (through Gorilla Context), stretchr's Goweb, Goji and others provide a `map[string]inteface{}.` Okay, so I still lose the type system, but now I have to litter my code with a ton of `realValue, ok := myMap["myValue"].(myType)`.

This is a good point, and it's something that—after you write a few pieces of middleware in any web application—you run into pretty often.

I avoid repeating things by creating quick Set and Get methods around that type that save me having to write out a type assertion; ok { } block every time, but there's still some repetition when it comes to dealing with the error.

Martini, for all it's "warts", is a fairly well thought out project and it's popularity is not an accident—even if it isn't idiomatic Go.

FWIW, I've started to look into Goji and like it a lot. There is /some/ interface{} there, but any type issues are at least caught on program initialisation (so you don't get bitten later), you get a request context without a global map/lock, and middleware is anything that satisfies http.Handler. I'm happy to give up a tiny bit of compile time safety* for a saner API and the extra things I've mentioned above. You generally have to be trying to give it the wrong type anyway, from my experience.

gocraft/web (as you have touched on) takes an interesting approach by having you create your own Context, which can then wrap any types you might pass around in your request context. The function chaining in the README is a little ugly, but you otherwise get a) type safety and b) no type assertions when dealing with context. The downsides seem to be a need to rewrite (to some extent) any middleware that already satisfies http.Handler to incorporate the web.NextMiddlewareFunc type instead, unless I'm misinterpreting that.

* Perhaps I'll regret saying this!