Learning Go (Golang)
A dive into a language from the search company with an unsearchable name.
Recently I started learning Go. Or Golang, if you want. Because “Go” is one of the most unsearchable programming language names.
Why did I do this?
I used Python as the server-side language for quite a while, since 2007. I think I had pretty decent skills in the language, but after a while it became more boring and more annoying. A few things bothered me every time I used it.
First of all, the unicode support. Every time I did something with strings I had to be very careful about it. I had to remember to write the correct unit tests, that check string parsing in different encoding. Howewer, the apps crashed because, for instance, somebody added some cyrillic characters in a configuration file.
Sure, python 3 solved the issue. But it is still not that widespread as it should be. There is a lot 2.x-only compatible code and you have to deal with it. The community even created a library called `six.py` which helps the script to be working with both Python 2.x & Python 3.x.
Runtime errors. There is no compiler, so any error you can get in Python is a runtime error. If you don’t have a code coverage close to 100% there still might be a case when some extremely stupid mistake will crash everything. IDEs like PyCharm help, but they cannot replace the compiler.
Therefore I decided to change my server-side language. I came up to some requrements I want the language to fullfil:
- Compile-time error checking. I do want the language to have as strict compiler as possible. I do want to catch as much errors as I can at a compile time.
- Out-of-the-box unicode support. This is probably not an issue for most of the contemporary languages, but it is important for me anyway.
- Good standard & 3rd party libraries.
- Simple deployment on a server. I don’t want to learn a lot to be able to deploy an app in a server.
I discovered Golang when Marco Arment was talking on a podcast about how he re-wrote part of his server-side code: ATP podcast.
I decided to give it a try, because the language fits well into the requirements I had:
- it has strong standard library, especially the parts that are helpful for server-side development;
- the community around the language is very active, there are lots of 3rd party frameworks;
- the language has a very strict compiler;
- the output of the compilation is a single statically-linked binary, therefore deployment of the app is relatively simple.
I learn Golang now I want to share the resources I found useful for learning.
First of all, I strongly recommend to listen to The Web Platform Podcast Episode 55. It is an interview with Andrew Gerrand, Go core contributor. It has lots of really awesome links in the shownotes.
The Go Playground (https://play.golang.org)
It is not a teaching resource per se but it is a very powerful tool. Here you can type in a code snipped, press “run” and the code will be executed. You can also share created snippets with others. These snippets are really popular among the StackOverflow users.
I’ve read 2 books on Go programming so far.
“An Introduction to Programming in Go” — a very simple and short book. It can be a good introduction to Go, it you are not a very experienced developer. It was a bit too simple to me, however it might be a nice book to scan through to get used to the language syntax and basic constructs.
“The way to Go”. This is by far more advanced book than the previous one. It is long and very detailed. The book starts with the basics but it covers a lot of topics in the language and the standard library. It also can be used as a reference.
Books help to know the features and capabilities of the language. But knowing the language is just one side of the coin.
The development community usually have “best practices”, containing the experience on how programs should be developed and what typical mistakes the developers make. You will master this while using the language, but something can be learned from the people who actually used the language in real projects.
Vincent Batts — Golang: The good, the bad, & the ugly — this is an introductory talk, you can learn some of the “unusualities” of the language even before starting to learn it.
How SoundCloud uses Go — a really nice video about how Go is used in real life by a big project.
One more way to learn the Go code is to study an open-sourced project. Go standard library is a good example to study first. Also there are a lot of interesing open source codebases to learn from.
There are a lot of golang project on GitHub right now. Go has a very open-source friendly community.
Where do I find a library doing X?
There are a few resources where you can find 3rd party libraries.
GoDoc — this is the place most of the golang libraries (including the standard library) store their documentation on. It is really cool for searching some specific “building block” for your app, because it searches through both standard library functions and 3rd party libraries.
This resource is provided by the core Go team itself.
* * *
I hope this post is helpful for both the people studying golang and people who already have the experience. Good luck!