Best Practices for Writing Clean and Maintainable Go Code

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler [8]

Writing clean code that is easy to test and maintain is much harder than it seems at first glance. Fortunately, Go, as a programming language, is quite opinionated and comes with its own set of best practices.

If you take a look at some of the available material for learning Go (for example, Effective Go [6]) or watch some talks by prominent members of the core Go team such as Rob Pike, it becomes evident that software engineers are gently nudged toward applying those principles when working on their own Go projects. From my perspective and experience, these best practices tend to have a measurable positive effect on the code quality metrics associated with a code base and at the same time aid in minimizing the accumulation of technical debt.

In this chapter, we will cover the following topics: