Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Katherine Cox-Buday

Rating No ratings yet

Concurrency can be notoriously difficult to get right, but fortunately, the Go open source programming language makes working with concurrency tractable and even easy. If you’re a developer familiar with Go, this practical book demonstrates best practices and patterns to help you incorporate concurrency into your systems. Author Katherine Cox-Buday takes you step-by-step through the process. You’ll understand how Go chooses to model concurrency, what issues arise from this model, and how you can compose primitives within this model to solve problems. Learn the skills and tooling you need to confidently write and implement concurrent systems of any size. ● Understand how Go addresses fundamental problems that make concurrency difficult to do correctly ● Learn the key differences between concurrency and parallelism ● Dig into the syntax of Go’s memory synchronization primitives ● Form patterns with these primitives to write maintainable concurrent code ● Compose patterns into a series of practices that enable you to write large, distributed systems that scale ● Learn the sophistication behind goroutines and how Go’s runtime stitches everything together

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, pattern-driven guide for Go developers who want to move beyond toy examples and build concurrent systems that are correct, maintainable, and scalable—covering everything from the mental model of goroutines to the sophisticated runtime that schedules them. 【Book Arc】 - **Opening (~0%–10%)**: Sets the stage by explaining why concurrency is hard in the modern era of multicore processors, cloud computing, and "web scale" expectations. It frames the book's mission: to give developers a comprehensive toolkit for wielding Go's concurrency features correctly. - **Early (~10%–23%)**: Dives into the fundamental challenges of concurrency—atomicity, deadlock, livelock, and starvation—using vivid examples like the "hallway shuffle" livelock. This section builds the vocabulary and problem-awareness needed before touching any Go-specific syntax. - **Early (~23%–32%)**: Introduces the theoretical foundation of Go's model, Communicating Sequential Processes (CSP), tracing its lineage from Hoare's 1978 paper. It explains why Go's design philosophy—"share memory by communicating"—is a deliberate choice with deep roots in process calculus. - **Middle (~32%–42%)**: Shifts to the core primitive: the goroutine. It explains the fork-join model, the M:N scheduler, and why goroutines are so lightweight. Crucially, it covers the "gotcha" that goroutines are not garbage collected, setting up the need for leak prevention. - **Middle (~42%–48%)**: Begins the deep dive into the `sync` package, starting with `WaitGroup` as the fundamental tool for joining concurrent operations. It also introduces `RWMutex` for read-heavy scenarios, establishing the pattern of choosing the right synchronization primitive for the job. 【Key Takeaways】 - **Concurrency is a design problem, not just a performance trick** (Early): The book's central thesis is that concurrency is about modeling your problem space correctly, not just making things run in parallel. This mindset shift is essential before writing any code. - **Atomicity is context-dependent** (Early): The deceptively simple `i++` is not atomic—it's three operations. Understanding that atomicity depends on the context (process, goroutine, OS) is the first step to avoiding race conditions. - **Livelocks are harder to spot than deadlocks** (Early): A livelocked program appears to be doing work (high CPU, active output) but is stuck in an eternal "hallway shuffle." This is a subset of the broader problem of starvation, which the book treats as a first-class concern. - **CSP is the intellectual foundation of Go's concurrency** (Early): Hoare's Communicating Sequential Processes model, with its `!` and `?` primitives for input/output, directly inspired Go's channels. Understanding this lineage clarifies why Go favors communication over shared memory. - **Goroutines are cheap, but not free** (Middle): A new goroutine costs only a few kilobytes and about three cheap instructions per call, making it practical to create hundreds of thousands. However, the garbage collector does not collect abandoned goroutines—leaks are your responsibility. - **The fork-join model is the mental map** (Middle): The `go` statement is a fork; synchronization points are joins. This simple model helps you reason about where your program's execution branches and where it must converge. - **`WaitGroup` is for joining, not for collecting results** (Middle): Use `WaitGroup` when you don't care about the result of concurrent operations or have another way to collect them. If you need results, the book suggests channels and `select` instead. 【Reading Tips】 - **Skim the history, but don't skip it**: The CSP background in the Early section might feel academic, but it's the key to understanding *why* Go's model works. Read it once for the "aha" moment, then move on. - **Deep-read the "Why Is Concurrency Hard?" section**: The examples of deadlock, livelock, and starvation are the book's most valuable content. They build the intuition you'll need for every pattern later in the book. - **Treat the code examples as experiments**: Don't just read the code—run it. The livelock example, in particular, is worth modifying to see how coordination (or lack thereof) changes the outcome. - **Watch for the "gotcha" moments**: The book is excellent at flagging pitfalls, like goroutine leaks and the non-atomicity of `i++`. Mark these sections; they're the difference between working code and production-ready code. - **Use the book as a reference, not a novel**: Once you've read the first few chapters, jump to the pattern sections when you need them. The book is structured so that later chapters build on earlier ones, but the patterns are self-contained enough for on-demand consultation. 【Coverage Limits】 The excerpts cover the book's opening through the middle of the `sync` package chapter (roughly the first half). They do not cover the later chapters on channels, context, and the runtime internals, which are mentioned but not detailed in this guide.
Page 9
e intuitive. The first time I used the go keyword to create a goroutine (something we’ll cover, I promise!) I got this silly grin on my face. I had worked wi...
View in text
Excerpt 2
her goroutines, then this code is atomic. So why do we care? Atomicity is important because if something is atomic, implicitly it is safe within concurrent c...
View in text
Excerpt 3
ion, between processes correctly (this is where the paper’s name comes from). Hoare applied the term processes to any encapsulated portion of logic that requ...
View in text
Excerpt 4
r 4 in the section “Preventing Goroutine Leaks” on page 90. We’ll use this to our advantage in the next example to actually measure the size of a goroutine....
View in text
Excerpt 5
me=10s -bench=. The sync Package | 63 This will error with: invalid operation: <-writeStream (receive from send-only type chan<- interface {}) invalid operat...
View in text
Excerpt 6
often think this function relates to the number of logical processors on the host machine—and in a roundabout way it does—but really this function controls t...
View in text
Excerpt 7
broader terms, we’ve successfully separated the concerns of error handling from our producer goroutine. This is desirable because the goroutine that spawned...
View in text
Excerpt 8
of the stages to chew through slow operations more quickly. This is a relatively simple example, so we only have two stages: random number gen‐ eration and p...
View in text
Tags
AI categories
GoProgramming LanguageBackend
ISBN: 1491941197
Publisher: O’Reilly Media
Publish Year: 2017
Language: English
Pages: 242
File Format: PDF
File Size: 4.3 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…