July 25, 2014 Tutorial – Building Go Web Apps
Following up on my last post, Ready… Set… Golang!, we’re going to go over some resources for learning Go and build a small web app and host it on Heroku.
I’ve spent the last couple of days digging into Go and getting the feel for it and so far I really like it. It’s different than most other languages in a number of ways. It’s not about object-oriented programming or functional programming. It about getting stuff done. Build times are almost negligible and the code runs super fast. It’s concurrency model is very powerful and it’s standard library provides almost everything you need out of the box. For a good overview of what Go is, how it was devised and why it is so cool, check out this blog post.
I started off with the interactive Tour of Go.
The tour is divided into three sections: basic concepts, methods and interfaces, and concurrency. Throughout the tour you will find a series of exercises for you to complete. Click the Run button for each section to compile and run the program on a remote server. No software installation needed to get start playing. If you are feeling adventurous, you can head over to the Go Playground and run your own code. Great for testing snippet you find or code you want to experiment with.
I found How to Write Go Code a god start as it demonstrates the development of a simple Go package and introduces the go tool, the standard way to fetch, build, test and install Go packages and commands. However, if you prefer, there is a video version instead.
A must read for any new Go programmer is Effective Go. The document gives tips for writing clear, idiomatic Go code.
If you prefer books (or PDFs of books), I found the following to be really helpful:
I think my favorite site is Learn X in Y minutes, Where X=Go. The site has one long Go file with a ton of effective commenting that teaches concepts along the way. I really love this site.
Go by Example is a hands-on introduction to Go using annotated example programs.
If you are fan of Railscasts, there a Gophercasts with a couple of good videos, especially for Postgres and Martini.
And finally Go (Golang) Pointers in 5 Minutes covers of course pointers in Go. Not surprising.
If you need a little help now and then, there is of course Stackoverflow and the go-nuts IRC channel,
freenode.net#go-nuts. If you are keen on Slack, Gohper Academy just announced a new Slack community you can join.
Building Topcoder ‘Cribs’
I learn best by doing so I looked around for something to build. I wanted something a little more than a
hello world but definitely not production quality. One thing we want to do with topcoder is allow members to post pictures and video of where they work, i.e., their cribs.
So the first thing I did was look around for a web framework for Go. There’s a great reddit thread with a ton of info. The Square Engineering blog also has an in-depth analysis as well with their winner. I finally decided to use Martini for a couple of reasons: 1) it smells a lot like Express and Sinatra so it was easy for me to grok, has a huge community and seems to be growing by leaps and bounds.
Server.go is where all of the action happens. It’s pretty small but straight forward and well documented so you can see what’s going on.
Here is the layout template the all of the views use. The HTML for each view is injected into the layout.
The home page iterates over the array of all returned Cribs from mongodb and links them to the display page. It also contains a form to post new Cribs.
Any finally the display page shows an image, youtube video or vimeo video based upon the type of Crib.
Deploying to Heorku
If you are deploying to Heroku you’ll need to add the following files. The Heroku buildpack needs to know where to put your code in the image. Add the .godir file in your project root with your directory structure:
You’ll also need a Procfile in your project root so heroku knows what type of dyno to spin up:
Finally, when creating your application add the buildpack flag and don’t forget to add the Mongodb and environment variables:
So now I have my first Go application running and am fairly happy with it. However, after building the app, reading this rebuttal of Martini and talking with a couple of Appirians that use Go, I’m thinking of scrapping Martini and using simply the standard Go library. Here’s an interesting blog post that I have been looking at regarding this.
In the next blog post, I plan on deploying my app to AWS with Docker.