100 Go Mistakes and How to Avoid Them
100 Go Mistakes and How to Avoid Them
Summary:
Introduction
The book "100 Go Mistakes and How to Avoid Them" (Teiva Harsanyi, 2022) is not just a collection of typical mistakes, but a full-fledged practical guide to writing high-quality, efficient, and maintainable code in the Go language. The author, an experienced engineer and consultant, has gathered 100 real problems that developers face and offers clear, well-reasoned solutions. Published in 2022, the book immediately became a bestseller in the "Golang" category. Across 483 pages, it covers all key aspects of the language: from basic constructs to advanced topics such as concurrency, memory management, and testing.
What the book is about
The book is structured into thematic sections, each dedicated to a specific area of Go development. Within each section, there are 5 to 15 specific mistakes with a detailed analysis. Each chapter follows a consistent template: a description of the problem, a code example, an explanation of why it is a mistake, and finally, the correct approach. This format allows the book to be used both as a textbook and as a reference. Main topics:
- Basic Constructs — variables, data types, control structures, functions.
- Strings and Collections — working with strings, slices, maps, pitfalls.
- Interfaces and Generics — proper interface design, typical mistakes with empty interfaces.
- Concurrency — goroutines, channels, mutexes, atomics, synchronization patterns.
- Memory Management — stack, heap, escape analysis, garbage collection, leaks.
- Error Handling — panic, recover, error values, wrapping.
- Testing and Benchmarking — writing tests, mocks, profiling.
- Standard Library — JSON, HTTP, file handling.
Who this book is for
The book is aimed at a broad audience of Go developers — from beginners who have already mastered the syntax to experienced engineers looking to deepen their understanding of the language's internal mechanisms. It will be especially useful for those transitioning to Go from other languages (Java, Python, C++), as many mistakes are related to incorrectly transferring habits. The book will also be an excellent resource for team leads and code reviewers who want to learn to spot problematic areas in others' code.
What the reader will learn
- Why you shouldn't use
rangeto copy slices and how to copy them correctly. - How to avoid data races when working with goroutines and channels.
- The difference between
sync.Mutexandsync.RWMutexand when to use which. - How to handle errors properly to avoid losing context.
- Why
defercan lead to memory leaks and how to avoid this. - How to write effective benchmarks and interpret their results.
- Typical mistakes when working with JSON (e.g., ignoring
json.RawMessage). - How to use generics (Go 1.18+) without unnecessary complexity.
- Why you shouldn't rely on the order of elements in a map.
- How to avoid panics in production code.
Strengths of the book
First and foremost — practical focus. Each mistake is accompanied by real code that can be run immediately to see the problem. Second — depth of explanation: the author doesn't just say "don't do this," but explains why it happens at the compiler, runtime, or standard library level. Third — structure: the book is divided into short chapters, allowing you to read it from any point. Fourth — relevance: it accounts for changes up to Go 1.19, including generics. Fifth — quality of translation (if referring to the Russian edition) — terminology is precise, examples are adapted. Finally, the book contains numerous links to the official do