Top 10 Most Imported Golang Packages — Some Insights

Having seen the cool things it’s now possible to do using using Google BigQuery’s public data sets (notably this), I decided to take a look at the top imported golang packages on a weekly basis.

Interested in finding the best logging solution for your go application? or perhaps you want to make sure you are using the right HTTP request multiplexer? What better way is there than good old democracy! It is usually the case that the most imported package will be the best one for the job.

Additionally, going over the most imported packages was a nice exploratory experiment, I got to learn about really cool things I should be using and still did not — notably Ginko and Gomega, who already changed the way I write my tests in Go.

I plan to republish this sort of post every couple of weeks, covering new packages that are a part of the most commonly imported packages.


Top 10 Imported Repos (this week)

The following are the most imported repos that are hosted on GitHub (the importing application is on GitHub, the imported package may be somewhere else like gopkg.in) as of today:

  1. Testify (testing)
  2. Kubernetes (container orchestration)
  3. Ginkgo (testing)
  4. Logrus (logging)
  5. Gomega (testing)
  6. glog (logging)
  7. gocheck (testing)
  8. AWS SDK (cloud tools)
  9. errors (error handling)
  10. cobra (productivity)

Out of 10, 4 repos are about testing and 2 are about logging. It’s clear that testing and logging are two subjects the community deeply needs to compliment the native Go packages.

If you haven’t yet heard about Ginko, logrus, errors or cobra mentioned above, I strongly recommend that you read on — I found them to be super valuable for me as a Go developer.


Testing is King

No less than 4 out of the top 10 packages are related to testing! It seems that Go’s native testing infrastructure is a fertile ground for enhanced testing frameworks, anything from simple assertion libraries to fully blown BDD frameworks with stand alone executables.

Testify and gocheck are simple but powerful libraries that provide mocking, assertions and more on top of the standard Go testing library. It feels like a natural (or even essential) extension to the standard testing library, which is super slim.

Ginkgo on the other hand is a more heavy duty BDD framework. Gomega is the matcher/assertion lib used by Ginkgo. In my applications I’ve chosen to use Gomega instead of going full BDD with Ginkgo for now, to keep it simpler. Here’s a tiny little example of a public repo I recently published that uses Gomega for JSON matching.


cobra is an awesome toolbelt / productivity suite for go applications. It helps you write a well-structured go application that’s easier to develop and maintain. It’s got a lot of work done around standardized usage of flags, bash autocompletion for your app, and more.

cobra was started by spf13, who is also responsible for other high profile go repos (notably hugo, a static website generator and viper, a great configuration solution for go apps, that can integrate with cobra). It’s worth noting that cobra is being used by the guys at Kubernetes, one of the greatest go applications on GitHub, with a pretty massive team behind it.

cobra features demonstration from https://github.com/spf13/cobra

Logging and error solutions

2 out 10 packages are logging packages, which basically extend the native log package behavior by providing useful features like color coding, formatting, log levels separation, etc.

Logrus is the more comprehensive of the two and it provides the basic logging extensions described above, plus some more neat features like hooks. From the logrus documentation:

“You can add hooks for logging levels. For example to send errors to an exception tracking service on Error, Fataland Panic, info to StatsD or log to multiple places simultaneously, e.g. syslog.”

glog is a logging library created by Google, and modeled after the C++ glog library. It’s a very simple, more basic log extension, which basically allows leveled execution of logs and not much else (a good thing from my perspective). From the glog documentation:

“By binding methods to booleans it is possible to use the log package
without paying the expense of evaluating the arguments to the log.
Through the -vmodule flag, the package also provides fine-grained
control over logging at the file level.”

errors is an outstanding library for better error handling in go. It’s got tons of features, but the most interesting for me were the means of handing errors that is almost identical to the standard way, however with two key differences: it allows you to keep the context of the error (files and line numbers) and attach some annotations of your own on top of it. Both are things I sometimes used to write on my own when trying to debug using error handlers.

From the errors documentation:

A primary use case for this library is to add extra context any time an error is returned from a function.

if err := SomeFunc(); err != nil {
return err
}

This instead becomes:

if err := SomeFunc(); err != nil {
return errors.Trace(err)
}

which just records the file and line number of the Trace call, or

if err := SomeFunc(); err != nil {
return errors.Annotate(err, "more context")
}

What’s Next?

I plan on re-visiting the most imported packages every couple of weeks and write about stuff I found interesting, so if you enjoyed this post, stay tuned!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.