Back
Go concurrency distilled: a free mini-book on Go concurrency with runnable examples
SiTech AI Team3 წთ. საკითხავი

Go concurrency distilled: a free mini-book on Go concurrency with runnable examples

Anton Zhiyanov has published “Go concurrency distilled”, a free mini-book that refreshes the whole Go concurrency toolkit, from goroutines and channels to context, mutexes, atomics and production diagnostics, with interactive examples.

Anton Zhiyanov, author of the Go-focused blog antonz.org, published the free mini-book “Go concurrency distilled” on 26 September. The author calls it a quick refresher on concurrent programming in Go rather than a beginner's course; those who want to learn concurrency from scratch, with practical exercises, are pointed to his earlier book “Gist of Go: Concurrency”. The new book is, in the author's words, AI-free.

The full text runs in the browser with interactive examples: the code in every snippet can be edited and run on the page. A PDF version with static examples lives in the nalgeon/go-conc-distilled repository on GitHub.

From goroutines to pipelines

The opening chapters cover the foundations: goroutines started with the go keyword, wait groups that synchronize them, and channels, which goroutines use to pass values to each other, in unbuffered, buffered, closed and nil variants. Next comes select, which multiplexes data flows, cancels goroutines and performs non-blocking operations.

A separate chapter is devoted to pipelines, where each step reads from one channel and writes to another, and to error handling: returning on the first error, packing an answer and an error into one result type, or collecting errors in a separate channel.

Time, context and synchronization

Time gets its own chapter: time.After for timeouts, timers for deferred work and tickers for periodic tasks, plus advice to reset one timer in a loop instead of creating a new one each iteration. Context, whose main job is to cancel operations manually, by timeout or by deadline, is explained together with its layered design: a child context can only shorten a parent's deadline, and multiple cancel calls are safe. The book also covers custom cancellation causes and context.AfterFunc cleanup.

The synchronization primitives follow: wait groups with the newer Go method, mutexes with TryLock and RWMutex, semaphores, condition variables with signal and broadcast, sync.Once, sync.Pool for easing garbage collection, and atomics. The author stresses the difference between a data race, which the race detector catches with the -race flag, and a race condition, which it cannot.

Testing, scheduling and diagnostics

For tests, the book suggests leaning on the synchronization handles the code already exposes, such as channels or types with a Wait method; where there are none, the synctest package runs a test inside an isolated bubble with a fake clock, so time-dependent checks execute instantly.

The closing chapters cover the scheduler, which spreads goroutines across OS threads (a goroutine needs about 2 KB of stack), and production diagnostics: the runtime/metrics package, the pprof profiler with CPU, heap, goroutine, block and mutex profiles, tracing with go tool trace, and flight recording, which keeps a recent trace in a sliding window.

SSiTech

SiTech — AI-powered web development

We build fast, modern websites and bring AI into real business workflows. Have a project or a question? We'd love to help.