Favourite golang ebooks
As a Go developer, I’ve found two ebooks that have significantly improved my understanding and mastery of the language. Here are my top picks and the key insights they offer.
1. Concurrency in Go by Katherine Cox-Buday
This book is the definitive guide to understanding Go’s concurrency model. It takes you from the basics to advanced patterns with clarity and depth.
Key Highlights:
Understanding Goroutines and Channels
- Goroutines are incredibly lightweight, with initial stack sizes of only 2KB
- The book explains the Go scheduler and how it manages thousands of goroutines efficiently
- Channels provide a safe way to communicate between goroutines, following the principle: “Don’t communicate by sharing memory; share memory by communicating”
The Select Statement
- One of Go’s most powerful constructs for working with multiple channels
- Enables timeout patterns, non-blocking operations, and graceful shutdowns
- The book covers practical patterns like the for-select loop and done channels
Concurrency Patterns
- Confinement: Keeping data safe by ensuring only one goroutine accesses it
- The pipeline pattern: Breaking down processing into stages connected by channels
- Fan-out, fan-in: Distributing work across multiple goroutines and collecting results
- The or-done pattern: Preventing goroutine leaks when operations are cancelled
Preventing Deadlocks and Race Conditions
- How to detect and fix common concurrency bugs
- Proper use of sync.Mutex, sync.RWMutex, and sync.WaitGroup
- Understanding when to use buffered vs unbuffered channels
The Context Package
- Managing cancellation across goroutine boundaries
- Passing request-scoped values safely
- Setting deadlines and timeouts for operations
2. 100 Go Mistakes and How to Avoid Them by Teiva Harsanyi
This book is a treasure trove of practical wisdom. It catalogs common pitfalls and explains not just what goes wrong, but why it happens and how to avoid it.
Key Highlights:
Slice Gotchas
- Understanding slice capacity vs length and how append() works
- The danger of slice sharing and unintended mutations
- Memory leaks from keeping references to large backing arrays
- Using full slice expressions
s[low:high:max]to control capacity
Map Mistakes
- Maps are not safe for concurrent use without synchronization
- The zero value for non-existent keys can lead to subtle bugs
- Order of iteration is randomized by design
- Using sync.Map for concurrent scenarios
Error Handling Best Practices
- When to wrap errors with
fmt.Errorfand%w - The importance of error types and error values
- Proper use of
errors.Is()anderrors.As() - Not ignoring errors in defer statements
Interface Design
- Accepting interfaces, returning concrete types
- Avoiding interface pollution by keeping interfaces small
- Understanding interface satisfaction and type assertions
- The empty interface
interface{}vsany(they’re the same)
Concurrency Pitfalls
- Forgetting that goroutines are not guaranteed to execute
- Not understanding happens-before guarantees
- Misusing sync.WaitGroup (especially calling Add inside goroutines)
- Creating goroutine leaks by forgetting to signal completion
Performance and Optimization
- Unnecessary allocations with string concatenation
- Understanding when to use pointers vs values
- The cost of interface conversions and type assertions
- Proper use of
strings.Builderfor efficient string building
Code Organization
- Package design and avoiding circular dependencies
- When to use init() functions (and when not to)
- Project layout best practices
- Proper use of internal packages for encapsulation
Why These Books Complement Each Other
“Concurrency in Go” gives you deep expertise in one of Go’s most powerful features, while “100 Go Mistakes” provides breadth across the entire language. Together, they transform you from someone who can write Go code to someone who writes idiomatic, efficient, and maintainable Go.
If you’re serious about Go development, both books are essential additions to your library. They’ll save you countless hours of debugging and help you write better Go code from day one.