Minna no Go Gengo: A Summary / Review in English (chapter 1)

My copy of Minna no Go Gengo, the Golang book with the cover everyone loves, came in the mail today!

Just last week, I was talking to some gophers based in Germany and when I mentioned that a coworker of mine recently published a chapter in a book on Go, one of the guys immediately asked "Is it the one with the gophers and robot on the cover?". Since the book only exists in Japanese I was surprised that he knew about it, but I guess I shouldn't be too suprised. The cover is just too good not to share, am I right?

So I thought i'd write a quick review / summary of each chapter in case there are some gophers out there who are interested in knowing what's in the book. I've just barely begun reading, but as the title, Minna no Go Gengo (Everyone's Golang), suggests, it covers a number of topics for a range of different skill levels from how to get started for absolute beginners to more advanced topics like reflection. Each chapter is written by a different author, all of whom are well-known OSS contributors here in Japan.

The first chapter is the most beginner friendly, but also contains some stellar tips about how to write Go code in a Go-like way.

How to start writing Go code for team development.

Author: Matsuki Masayuki (aka @Songmu)

This chapter starts out with the essentials: how to install Go, an introduction to some of the core command-line tools used in Go development as well as suggestions for some useful third party tools like ghq, peco and glide). It's super concise and does a great job of covering the essentials without being too verbose.

For anyone who's written Go code before, the meat of the chapter, though is in the style guide which highlights some differences between writing programs using scripting languages like Ruby and Perl vs writing in Go. For example:

  • Avoid using regexp: use the strings package wherever possible instead. Why? They can be really slow, sometimes even slower than Perl regexp... which is pretty bad for a pre-compiled program.
  • Avoid maps. Because Go is a strongly typed language it is better to use structs. Also maps are not thread-safe. If you need to use a map alongside concurrency embed one in a struct alongside a sync.RWMutex.
  • Don't overuse concurrency. While Go is great for concurrency overusing it not only makes programs harder to read, but also increases the likelihood of race conditions.
  • Use the -ldflags and -tag options to embed useful information in a binary when using go build.
  • runtime.NumGoRoutine and runtime.ReadMemStats are useful monitoring metrics for web servers and other long running programs. golang-stats-api-handler is a useful library that provides an api interface to the go runtime package.

Hardly an exhaustive list, as this chapter is packed with useful info for people who are transitioning to Go from other languages and gives a good introduction to how to get into a Go mindset. I am looking forward to reading and writing up on the remaining chapters.