6 ms·
Building Web Apps with Go
- john2x 12y agoIf there's one thing Go did right, it's the logo/mascot. (sorry for OT)
- icebraining 12y agoI still prefer Plan9's.
- dscrd 12y agoWell, they had an actual artist do it.
- nkozyra 12y agoThe same actual artist that did the Go mascot also did Plan9's. https://golang.org/doc/faq#Whats_the_origin_of_the_mascot https://golang.org/doc/faq#Whats_the_origin_of_the_mascot
- kbart 12y agoI keep giggling every time I see it, because it reminds of Pedobear[1] 1. http://en.wikipedia.org/wiki/Pedobear http://en.wikipedia.org/wiki/Pedobear
- wosos 12y agoReminds me of a popular 4chan/reddit draw, you just need to add the 'tail'.
- humanfromearth 12y agoAren't there a lot of required packages to build a webapp? I would recommend against using any of those at least in the beginning. Maybe gorilla/mux, but even that can be avoided. Don't just add deps you will never use, it's going to make your life painful.
- regecks 12y agoIn Go its pretty difficult to build an application with unused dependencies - the compiler complains
- Rapzid 12y agoSo you end up with a lot of these: _ 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.
- sagichmal 12y agoYou should never write Go code by swallowing errors first and promising yourself you'll clean it up later. You should be coding the error path first, and backfilling your business logic. The error path directly informs your method definitions and the structure of your code, which resist modification once they're in-place.
- joeshaw 12y agoUse `goimports` as a save hook in your editor. It analyzes your code and inserts/removes import lines as needed. 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.
- buro9 12y agoFor the example given, you probably don't need Negroni, Controller or Render. 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 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-content 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.
- shadowsun7 12y agoI tried to post this review on gitbooks.io, but couldn't. So I guess I'll put this up here: 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.
- zkanda 12y agoI feel you on this, also coming from Django background, there is no equivalent yet for golang, though I've been skimming with revel for some time, it's still incomplete from what I want. This is just perfect opportunity to learn something that is more disconnected, but syncs with harmony. :)
- john2x 12y ago> 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." We need something similar for Clojure web apps, since they both favor libraries over frameworks.
- deleted 12y ago[deleted]
- xaritas 12y agoI haven't read it, but the developer of Luminus (a collection of libraries, not a framework) authored this fairly recently: Web Development with Clojure: Build Bulletproof Web Apps with Less Code (https://pragprog.com/book/dswdcloj/web-development-with-clojure https://pragprog.com/book/dswdcloj/web-development-with-cloj...) Luminus: http://www.luminusweb.net http://www.luminusweb.net Edit: argh italics
- krat0sprakhar 12y agoThis looks awesome! Thanks a lot, Jeremy for doing this and sharing it with us!
- JoeAcchino 12y agoIs this guide specific for Go on Heroku or its concepts can be easily applied elsewhere?
- truncate 12y agoIts just deployment which can be a bit different and its a small part of everything. Libraries are same, whether you deploy on Heroku or AWS.
- akbar501 12y agoIt would be helpful for Go newbies if a 3rd column (description) was added to the table in the "Required Packages" section.
- codegangsta 12y agoGreat idea. Added an issue https://github.com/codegangsta/web-development-in-go/issues/5 https://github.com/codegangsta/web-development-in-go/issues/...
- melling 12y agoI run a simple Go server behind Apache for my weekend project (http://www.thespanishsite.com http://www.thespanishsite.com). I started with this blog: http://www.jeffreybolle.com/blog/run-google-go-web-apps-behind-apache 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-swap-on-ubuntu-14-04 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-and-static-content-from-root 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) }) }
- melling 12y agoUPDATE: I can longer edit my original comment. 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 https://github.com/melling/GoWebServer There are a couple of scripts to build and run the server.
- andrewksl 12y agoCheck out supervisor (http://supervisord.org/ http://supervisord.org/). Super painless daemons if you're like me and hate writing init scripts.
- falcolas 12y agoThe hard-coded dependencies on GitHub (not to mention they're dependencies to code owned by someone else) have always bothered me. It seems like it would create a real problem for compiling, auditing, or even just testing code in the long run; and this example relies on a ton of them. I haven't kept up with the state of the art Go packaging; have these problems been addressed?
- sethammons 12y agoVendoring is the approach taken by many. Personally, I use godep. It vendors into your source the different libraries that you are using at a specific version (git hash in all the cases I use). This handles the compiling and locks in the particular version of the library which you have presumably audited. As far as testing goes, as long as you are making use of interfaces, testing should be easy enough.
- jpdlla 12y ago+1 for godep
- codegangsta 12y agoThanks for the feedback guys! The book is still a work in progress. It started as curriculum for the workshop I gave a couple days ago at DotGo. The plan is to continue making it awesome and to make the examples more complete.
- preillyme 12y agoIs using gorilla/context really the only easy way to store an retrieve data that is specific to the current HTTP request? Seems like there should be a better way to map values and retrieve them later from a global mutex on a map of request objects, or something.
- rabble 12y agoI don't mean to be a bit difficult here, but i wonder if the tutorial's author had anybody actually try it. I mean it seems like there's tons of missed steps, sections missing, assumed bits which aren't done. Each example i try tends to have other things done, uh, as exercises for the reader, which are required before the code samples even work.
- touristtam 12y agommm my feeling as well. Trying out on a Windows 7 machine (sic) at the moment and heroku is complaining about MySql. Might have been an idea to deal with the deployment later during the tutorial....