i'm steve

Getting Your Feet Wet With Go

Sun 30 Aug 2015

GO is an open source programming language created at Google which has been getting alot of traction recent years. It aims at becoming a language that’s easy to write, efficient, built-in concurrency support, and garbage-free, yet also able to cross-compile for different platforms.

Why the HYPE

I guess…

  • It’s developed at Google, so golang is already blessed.
  • With Google’s experience in building things at scale, the language design should have already considered the scalability & concurrency perspective.
  • Bundled with GO is a large & useful standard library, and it’s very likely that what you need is already there. (e.g. some of the useful library includes net/http, fmt, encoding/json)
  • Of the language itself, it has standardized styling as a language feature, thus code are easier to read. This is important, as relatively speaking, most of the time are spent reading code instead of writing.
  • GO as a new language is fast to write, fast to compile and fast to run. These can’t be addressed well by libraries or tools, but the language specification & design.
  • GO make deployment extremely easy, given it can do cross-compilation for different platforms. With the compiled binary, even if the target system has no GO installed, it will still work simply.
  • Community & Public Adoption are important for a new language. As for GO, there’re already some big developer communities particularly in China (1, 2); more importantly, lots of companies have already adopted golang, e.g. Docker, Ubuntu, CloudFlare, SoundCloud
  • Trendy!

Getting Started

Enough said about the golang goodness. To get started, one can simply grab the GO Binary, and then visthe GO Tour for the introduction on the language.

There’s two important environment variables that I would particularly want to talk about here. You will normally come across them when doing the initial setup.

GOROOT – This is the path where to find the golang standard lib as well as the golang related binary. Normally it will point to “/usr/local/go/” for Mac / Linux.

GOPATH – This is the path which serves as development workspace, and also where to look for packages & dependencies. (e.g. those import you done within your go code, this is the directory to look for those import packages)

Having done the setup & learnt the language basic, I would recommend reading the Go by Example next. It’s an awesome learning material that is free & open source. The examples are nicely annotated & are carefully crafted to provide intros on various language topics. After that, some complete projects such as Gogs (Github clone in golang), Docker Project would be a good read.

Last but not least, start building something! There’s no better way to master the language than actually using it to do some real-world project.

Useful Tools

Like any other language, good tools can help the entire development even further. Here’s few that I have came across.

  • gin is for live reloading GO web application which is extremely helpful during development
  • gofmt is a built-in command and is really helpful in making sure everyone’s coding style are the same. So it’s highly recommended to run this command on the source files whenever its being saved. (IDE can be configured to run the command automatically)
  • goimports can help cleanup the import lines – adding missing imports and removing unused one.
  • oracle is a go source analysis tools, which can answer your query regarding the source code. Say, finding a caller or callee of a functions; describe the kind, type, value and whatever useful information it can find about an expression or variable; showing all the possible send / receive for a given channel operands etc. To know more about its capability, the user manual is useful.
  • Go Walker is an useful site for browsing API documentation. In short, Go Walker = Github README + GoDoc
  • GVM is like RVM to ruby, NVM for nodejs. It’s a Go Version Manager, in which, developer can use it to manage multiple active version of GO, as well as manage GOPATH settings, in turns multiple workspaces.
* To look for more useful tools, here’s a good place to look.

Quick Thoughts

With the above setup, you can already start writing go code. It’s certainly a language worth spending some time to investigate further. IMHO, web service (API server) or backend processing service should be a pretty good use case for GO, and it definitely will be my upcoming choice if I am to write one.