As already dicussed here, the amount of web framework for Go is pretty huge. Spontanously I can think of Beego, Gin, Buffalo, Echo and Revel, but I'm sure there are even more. Personally I usually prefer having a framework over writing everything from scratch, but it's really hard to decide for one. Does anybody have a table-like comparison of Go web frameworks with focus on their features (and whether they're still maintained or not)?
评论:
aboukirev:
Besides the fairly universal speed benchmark like https://github.com/julienschmidt/go-http-routing-benchmark I have not seen any feature comparisons.
One big difference in router type is that some routers require unique matches for a route (like
httprouter
, originalchi
). Others allow for multiple matches and utilize priority of some kind to pick the route: order of declaration or exact match is preferred over parameterized or wildcard (likeecho
).I have one Web application converted from Drupal where aliases (a fixed manually defined URL) were used for SEO purposes. Drupal first performs a lookup for an alias, translates to regular URL and only then passes that to router. I tried to do that in Go with custom middleware, custom handler, doing it in 404 handler (search alias when URL did not match any regular route). It was all ugly. Besides, it would make an extra database hit on most requests. I ended up using
echo
, loading aliases once on startup, and creating fixed routes that have highest priority. All in memory in a trie structure and routing is very fast.Before I picked
echo
I went through:
pure
net/http
Gorilla toolkit
gin
goji
echo
I could have stayed with gin
except it was lagging in adopting standard context
package at the time.
AFAIK, revel
stands out by doing everything in its own way, completely non-standard, non-idiomatic.
To add convenience and accommodate passing parameters from route parsing many Web frameworks did one of the following:
add an extra context parameter to all handlers
extend request type (custom request)
extend response type (custom response)
"Pure" frameworks now utilize context
to pass data around now (which is not what context
was designed for) or provide utility methods to parse route and URL query again in the body of a handler. I am talking about the routes like group1/group2/:id
where elements have been matched and ':id' parameter value parsed while routing and could have been immediately available to the handler except for the standard Handler and HandlerFunc having no way to pass that information in arguments.
There is more, of course. Standard net/http
does not define constants for some standard HTTP headers. Web frameworks introduce various convenience methods. All these things you can add in your own code. Frameworks also provide form binding and validation infrastructure, integrated logging, graceful shutdown, integration of template rendering (loading and caching templates), and some standard middleware: serving static content, CORS, sessions, etc. You can get those from separate 3rd party packages as well and typically frameworks just wrap 3rd party code.
The bad news is you have to try a few solutions to decide what you want from a framework.
Yojihito:aboukirev:loading aliases once on startup
What does happen if I create a new blog entry with an alias? Do I need to restart the router then?
kamaleshbn:It was for legacy routes. You can add dynamically routes in
echo
now while it's running. An alternative would be graceful restart.
I've seen this, but not exactly feature comparison. Still, a decent overview.
https://github.com/mingrammer/go-web-framework-stars
Also this is something which I wrote, it's bare bones, and sticks to standard packages, including the signature of the handlers.
