Building Web Apps with Go
codegangsta.gitbooks.io
codegangsta.gitbooks.io
This is a great introduction to building web apps in Go. (I started roughly two months ago, but had this book been around, I'd have been brought up to speed a lot faster).
Here's why: the predominant approach to building web apps in Go is to build on top of standard interfaces (e.g. net/http), and to keep things as simple as possible. Heavy, prescriptive frameworks are frowned upon. This is a great approach, but probably strange to people (like me) who come from prescriptive frameworks like Rails or Django.
Jeremy's guide sticks to Go conventions, while respectfully suggesting lightweight libraries that complement this approach. The guide is never "YOU MUST USE THIS", instead it always introduces the bare-bones approach first, and then tells you "hey, there's a 3rd party library that gives you some useful shortcuts on top of those." And indeed, each of the recommended libraries are idiomatic and easy to understand.
My review is probably biased, though, because I now have some idea now of how to write web apps in Go. But I certainly wished this book had existed when I first started.
Where you may see people discouraging a framework is when you ask how to do something very basic and attempt to use a framework to solve it. In those cases, you lose the chance to learn exactly how the underlying processes work natively in Go, and since you're already writing in Go, you might as well do it from scratch. Plus all of your code is very reusable going forward.
But if you know how it works and want something quick and fuss-free for a full application nobody will steer you away from the better frameworks.
Further reading for those interested:
http://www.programcreek.com/2011/09/what-is-the-difference-b...
http://tom.lokhorst.eu/2010/09/why-libraries-are-better-than...
I say if that's actually the case in the Go community, but I can understand getting that feeling starting out. I'm just starting with Go myself, and I had the same thought after some casual research digging through various blogs, tutorials and library suggestions.
As a newcomer to the language, it seems that there's a drive toward things being very explicit, using very little "magic", and composing smaller libraries, rather than using monolithic frameworks; all of that appeals to me.
If I'm way off base on that, I'd be happy to hear where my understanding is underdeveloped. Like I said, this is just what I get at first blush.
All in all, I'm really interested in checking out the book, and learning more about Go.
It's just that writing a good web framework is actually way more complex than just a router.Martini,Revel or Gorilla are not webframeworks.
I believe Go is too rigid to allow something like Rails or Django. Go is for people who want C like performances with memory safety and easy concurrency. Go is not a "typesafe" python, at least it doesnt work like one until now. But I wouldnt be surprised someone comes up with a language built on top of Go that looks like a "typesafe" python or a Go++. That's what happens when people dont get their "nice things".
The question is, could somebody build a strong framework for web applications on Go? Maybe, i'm still learning the language myself so i don't know. Just because the language designers don't like it, doesn't mean that somebody won't come along and do it.
We need something similar for Clojure web apps, since they both favor libraries over frameworks.
Web Development with Clojure: Build Bulletproof Web Apps with Less Code (https://pragprog.com/book/dswdcloj/web-development-with-cloj...)
Luminus: http://www.luminusweb.net
Edit: argh italics
http://www.jeffreybolle.com/blog/run-google-go-web-apps-behi...
I also use MySql on Digital Ocean with a $10/month droplet. The few issues at first where that I started with a $5/month which didn't enough RAM so I'd run out of memory until I created swap:
https://www.digitalocean.com/community/tutorials/how-to-add-...
Still need to make it a daemon, but I'm not finished. I have one big method to set up my pages. I could write a blog, github repo or create a summary page on my site, if there's any interest.
func runWeb() {
serveSingle("/robots.txt", "./robots.txt")
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css/"))))
http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("./resources/"))))
http.Handle("/static", http.FileServer(http.Dir("./static/")))
http.HandleFunc("/chinese", chineseHomeHandler)
http.HandleFunc("/french", frenchHomeHandler)
http.HandleFunc("/chinese/numbers", chineseNumbersHomeHandler)
// Many handlers deleted ...
http.HandleFunc("/", homeHandler)
// http.ListenAndServe("localhost:9999", nil)
port := GetPort()
fmt.Println("listening...", port)
err := http.ListenAndServe(port, nil)
if err != nil {
panic(err)
}
}/* http://stackoverflow.com/questions/14086063/serve-homepage-a... /
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
})
}I realize that a bunch of unformatted code on HN is not that useful so I created a quick starter Go Webserver on github: https://github.com/melling/GoWebServer
There are a couple of scripts to build and run the server.
Don't just add deps you will never use, it's going to make your life painful.
Mux is also possibly surplus, but does tidy up the extraction of values from routes a little. But to be totally honest the given example isn't complex enough to start letting Mux shine (multiple values on URLs, a single app serving both http and https).
Blackfriday (for Markdown) is necessary if you want to convert your text input into HTML, though there is a missing dependency here which is Bluemonday.
Bluemonday ( https://github.com/microcosm-cc/bluemonday ) is a HTML sanitizer and ensures that XSS supplied via the Markdown box is stripped before rendering - I wrote that, it's based on the whitelisting approach as demonstrated by the OWASP Java HTML Sanitizer. Blackfriday even recommends you clean your untrusted inputs: https://github.com/russross/blackfriday#sanitize-untrusted-c...
SQLite3 is required, or at least some SQL provider is going to be required if the tutorial wants to fully demonstrate actually saving content and using one of the established interfaces (database/sql) to do so.
So you could reduce this to three deps: Blackfriday, Bluemonday, SQLite3 and stick to core/stdlib http for the actual "web app in Go" stuff.
The other deps, it is debatable whether or not they are essential, certainly the larger web apps will find that they are simplified and easier to read and work with by using some combination of Mux, Negroni and Controller depending on your needs. Render I have not used and do not have a strong opinion on.
Without your comment the book would've done me more harm than good - now that I'm aware of the blanks, I can learn with the awareness that I'll have to assume similar errors lurk in the other sections (or: if it looks a little too simple, it's probably because corners were cut, going from simple to simplistic).
Which is okay, they act as a todo list of sorts. Search your program for _ when you feel like cleaning up. And you can handle all those errors you've been swallowing while you're at it.
Other than the occasional need to fix up an ambiguous import that it guessed incorrectly, I haven't touched import lines in my code in a year.
I haven't kept up with the state of the art Go packaging; have these problems been addressed?