Zen Go (Pocket Version)

In anticipation of the start of the “Golang Developer” course , we prepared a translation of a small useful note.




Ten technical tips for writing simple, readable, and easy-to-maintain Go code. Presented as part of GopherCon Israel 2020 .

Each package has only one purpose.


A properly designed Go package should only implement one idea, introducing a set of related behaviors. A good Go package starts with a good name. Try to make the name a brief presentation of your package, describing what it provides using only one word.

Explicit Error Handling


Reliable programs consist of elements that handle errors before they can hit them in the back. The verbosity of if err! = Nil {return err} is justified by the importance of well thought out handling of each error in the places where they can occur. Panic and recover are not exceptions; they are not intended to be used in this way.

Prevent deep nesting with return


Each time you indent, you add one more condition to the programmer’s mental stack, occupying one of the 7 ± 2 slots in his short-term memory. Avoid multi-level padding control flows. Instead of deep nesting, keep the successful code path as far to the left as possible using boundary operators.

Leave concurrency to the caller


Let the caller choose whether he wants to run your library or function asynchronously, instead of imposing this choice on him. If your library uses multithreading, it should do it transparently.

Before you start goroutine, ask when it stops.


Gorutins have resources; locks, variables, memory, etc. The most reliable way to free these resources is to stop the goroutine that has them.

Avoid package level properties


Strive for transparency, reduce connectivity, and avoid horrible remote actions by providing the dependencies that the type needs as fields of this type, rather than using package variables.

Simplicity matters


Simplicity is not synonymous with primitiveness. Simplicity does not mean incompleteness, it means readability and maintainability . If you have plenty to choose from, always lean towards the simplest solution.

Write tests to capture the behavior of your package API.


Whether you start with tests or end, strive for 100% test coverage or are satisfied with the necessary minimum - regardless of all this, the API of your package is your contract with users. Tests are a guarantee that these contracts are clearly stated. Make sure you test behavior that users can observe and rely on.

If something seems slow to you, first prove it using the benchmark.


Many crimes against sustainability are committed in the name of productivity. Optimization destroys abstractions, exposes the insides and tightly binds. If you decide to take on this burden, make sure that there are real reasons for that.

Moderation is Virtue


Use goroutines, channels, locks, interfaces, embedding with a sense of proportion.

Support matters


Clarity, readability, simplicity are all aspects of sustainability. Is it possible to support what you worked hard on after you left? What can you do today to make life easier for those who succeed you tomorrow?



Learn more about the course.



All Articles