Go packages that I’m using in every (almost) Microservice

Rafał Gałus
4 min readMay 27, 2021

I thought it can be beneficial to share a list of Go packages that are popular in my projects. The list consist of modules used for parsing environment, testing code, logging, database clients and others. You might know all of them but there could be one that makes it worth for you to get through the list.

Environment

There are two ways my services get the configuration. One is through environment variables (it’s used on production in k8s deployment) and second through the file (when I’m working locally). For the first one I’m using https://github.com/caarlos0/env package. It is pretty simple, it validates your configuration with different Go types. Second one is https://github.com/joho/godotenv which loads environment variables from the file.

Together they can be used as follows, both on production and locally:

Logging

Since most of my projects are deployed to Google Cloud, logs needs to be displayed well in the Stackdriver there. For that I’m using https://github.com/blendle/zapdriver which is based on https://github.com/uber-go/zap. There are other options available like logrus but I found zap very useful and easier to work with in my environment.

Testing

Tests are pieces of code that I’m spending a lot of time with. It is important to me to have tool that is easy to use and that helps me to write various types of tests including unit and integration fast. For all of this I’m using https://github.com/stretchr/testify. It is a toolkit that provides sub packages like assert, require, mock that I’m using as well with suite package which is very helpful to setup integration tests environment.

The code I’m writing usually have some or many different layers. It’s kind of hexagonal architecture so it have interfaces to work with. To easily test different layers of code I’m using https://github.com/vektra/mockery that automatically generates mocks for me based on those interfaces. It is very easy to use with comments and go generate command.

With this simple comment you can run go generate and it will create a mock file for you in ./mock folder.

Here is very basic example of using both packages and generated mock.

The last package that I’m using related to testing is https://github.com/gotestyourself/gotestsum . This one is very helpful if you need to generate JUnit XML file for you CI system.

MySQL and PostgreSQL

If you don’t have specific database requirements there is a huge chance that you use SQL database because of its popularity and being well know among developers. As a PostgresSQL driver in Go I’m using https://github.com/lib/pq with https://github.com/jmoiron/sqlx extension which is very popular and offers extended scanning (to Go struct for example). Together they provide very easy way to work with the database.

When deploying an application as a part of the workflow we run database migrations. For these and other needs package https://github.com/golang-migrate/migrate will cover your needs. It offers CLI (which I’m using in the deployment workflow) and in code used as a library. The latter can be used in integration tests, together with mentioned earlier testify library and its suite package, like below:

The last package I’m using in some cases related to database querying and SQL building is https://github.com/Masterminds/squirrel. It can be very helpful when you need to dynamically generate SQL statement. Package can be considered as “complete” and it’s not really maintained at the moment.

Apache Kafka

For those who are working with Kafka I highly recommend https://github.com/confluentinc/confluent-kafka-go package that is maintained by the same guy who created librdkafka. It is easy to write consumers and producers using this package. If you have more operational needs like creating topics then https://github.com/segmentio/kafka-go is my choice. It helps me setup integration tests environment.

If together with Kafka you’re using AVRO then to communicate with schema registry I’m using https://github.com/lensesio/schema-registry package. It is archived at the moment but still usable. For decoding and encoding AVRO https://github.com/linkedin/goavro is a good choice and the choice I’ve done for a long time. But after that I switched to https://github.com/actgardner/gogen-avro package that generates code needed to encode and decode Go types into AVRO. That really saved my time developing applications and writing tests so I personally recommend this one.

Other

Another helpful package is https://github.com/pkg/errors. It can be used to wrap one error into the other and offers other methods as well. Makes it easier to do error handling. This can be fixed in future versions of Go but at the moment it’s still helpful.

And also https://github.com/segmentio/ksuid to generate unique object IDs and https://github.com/cenkalti/backoff to implement retry logic in various of places.

--

--

Rafał Gałus

software development engineer with passion for coding