Something Similar

About Jeff Hodges
Atom Feed
Archive

Code to Read When Learning Go

When folks ask me how they should learn Go, I usually say:

Run through the activities in A Tour of Go, then read Effective Go (and maybe check out Go By Example), and then read some code.

This seems to work well for them. But the last part could use some examples of good, clear, idiomatic code to read. Let me do my part there.

Now, I’m holding these up as someone who already knows Go, didn’t use all of these to learn Go, and, perhaps worse, came to Go already knowing how to program. But even with these caveats, these are codebases that are useful to learn from.

pat (godoc) - Pat is a cute, small library that provides Sinatra-like HTTP handling in Go on top of the already fairly easy to use net/http Handlers. It’s a breeze to read. Feature-ful Go projects often have just a handful of files in them, gathering power from they way they combine the standard library in a particular convenient and narrower fashion than the original API. Pat is a great example of that (and Go’s stdlib being good enough for production is to be commended). It integrates well with other code you’ll use simply by relying on the interfaces in the net/http library.

codesearch (godoc) - To see how a larger project can fit together, I recommend the codesearch project. It’s use of the regexp package’s abstract syntax tree API to implement regexp search is wonderful. For maximum understanding, pair this code with Russ Cox’s articles on how to implement searching with regular expressions in queries. I don’t want to undersell that article. It describes how Google’s (now defunct) Code Search solved that problem and how codesearch, the Go implementation Russ wrote, works. The whole series of articles on regular expressions is good, but this one article is especially fun. My workplace shipped this project internally and it’s pretty much Solved™ our code search problems.

groupcache (godoc) - Groupcache is another larger codebase for those interested in that, and contains plenty of goodies within. It’s part of the dl.google.com, Blogger and Google Code architecture, and is, in many ways, a “smarter memcached” as a library. The README details its design. From its ownership model designed to avoid thundering herds, to the singleflight implementation of request collapsing, to the interface for extracting stats, groupcache is a maturely designed piece of software. Not just good code, but wise distributed system design is inside.

net/http - The net/http library is very Go-y, and used everywhere in the Go community. It’s been hardened for production use at scale and HTTP 1.x is a very hard protocol to get right, so it’s not always the easiest read. That said, it’s worth learning how such a common building block was designed. It also provides capabilities like net/http/pprof that takes Go’s built-in profiler and makes it available over the web. (Reading Profiling Go Programs is really nice, by the by. Be sure to check out the goroutine blocking profiler.)

There are so many more projects I could talk about. Especially ones using the Go AST tooling (like in godoc.org‘s gddo codebase), or the surprisingly clear crypto libraries (like crypto/subtle) but time and space won’t permit it. Poke your nose around, search for projects on Go Search, and write some code. One of Go’s major selling points is it’s readability, even to those inexperienced in it, and just digging in is worth your time.

(And, if you do nothing else, read Effective Go.)