Go has a "built in" framework in the way of its net/http
(http://golang.org/pkg/net/http/) package—think Ruby's Rack interface on
steroids.
- *Requests are served concurrently by default (each request lives in
its own goroutine)*
- Routing is easy:
package main
import "net/http"
*func main() {
* r := http.NewServeMux() r.HandleFunc("/", IndexHandler)
http.ListenAndServe(":8000", r)}
func IndexHandler(w http.ResponseWriter, r *http.Request) { // Do whatever you'd like here - e.g.
w.Write([]byte("Hello World"))}
- Everything is centered around the http.Handler interface - you can
make a custom handler type that returns a status code and an error, and
write a ServeHTTP() method (to satisfy the interface) that handles the
error and serves a standard error page based on 404|500|etc
- There's toolkits and micro-frameworks like Gorilla
(http://www.gorillatoolkit.org/), Goji (https://goji.io/) and gocraft/web
(https://github.com/gocraft/web/) that give you options for improved
routing (named params), request contexts (passing data alongside a request,
like Django's request.META dict), sessions and other middleware.
- Go does not include any "standard" middleware like Flask might, but
that's because Go typically aims to be composable (i.e. you're the glue).
But thanks to the aforementioned http.Handler, it's easy to write stuff and
share it: gorilla/handlers has some default logging & gzip handlers that
work with net/http (and anything that respects http.Handler) and I can plug
my own little project that does HTTP Basic
Auth:
https://github.com/goji/httpauthWhat Go might not give you—because the community is (mostly) focused on
modular components—is something that's "kitchen sink" like Django. If
you're trying to get a CMS or news site out the door in < 6 months and
don't need the performance that Go gives you any time soon, then Django is
probably going to help you get to your MVP faster.
Go, however, is likely going to be easier to maintain in the long run—the
"productivity" (hate that term...) vs. performance pay-off is very high.
Most of the "frameworks" are closer to something like Bottle or Pyramid
than Django or Rails (and you'll find most here will agree that that's a
positive thing).
Hope that helps.
On Thursday, July 10, 2014 5:06:30 AM UTC+8, Sean Bigdatafun wrote:Any thing widely adopted in Go community for web tier? The following
factors should be the primary consideration (at least for me):
- separate of concern, i.e., providing a high quality MVC framework (URL
routing is normally a one-liner, see the example below)
- easy way for authentication ( the web programmers normally use one
liner, see the example below)
- other things I missed here, more discussion / comments are welcome
Here is a Flask (Python) example:
@app.route('/secret_page') # one liner for URL
[email protected]_required # one liner for authenticationdef secret_page():
# My service logic...
pass
As one of the sweet spots for Golang is its concurrency capability,
Tornado/Twisted like framework can handle async request much efficiently
(but still programming them needs a certain learning curve). The framework
I'd like to try out in Go is the one that give me programming friendliness
as I described above using Flask code as example (i.e., MVC and sequential
logic thinking), yet giving me the async web request serving capability
(hopefully with same friendliness).
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
[email protected].
For more options, visit
https://groups.google.com/d/optout.