Kick off your book project, get started with GhostAI, get better at marketing, or spend the day doing all three! Free live workshops on Zoom. Saturday, June 27, 2026.

Leanpub Header

Skip to main content

GoLang并发编程之美

数据竞争、死锁、内存可见性问题从不提前打招呼,它们藏在代码里,等到生产环境才爆发。本书从底层彻底拆 解 Go 并发模型,让你建立起系统性的认知框架,第一次就写出正确的并发代码。

Minimum price

$9.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

并发编程是软件开发中最具挑战性的课题之一。编写正确的程序已属不易,编写正确的并发程序更是难上加难——数据竞争、死锁、内存可见性……这些问题隐蔽、随机,极难复现。

Go 语言自诞生之日起就将并发视为一等公民。它没有沿袭 Java、C++ 以共享内存为核心的并发模型,而是借鉴 CSP 理论,以 goroutine 与 channel 作为基础抽象,提出了那句影响深远的哲学:

不要通过共享内存来通信,而要通过通信来共享内存。

本小册从这一哲学出发,系统梳理 Go 并发编程的完整知识体系。全书分为五个部分,共 14 章 + 3 个工程实战案例:

  • 并发基础:从哲学背景到数据竞争、原子性、内存同步三大核心难题;
  • Go 运行时基础:深入 GMP 线程模型与 Go 内存模型 Happens-Before 原则;
  • 同步原语:goroutine 生命周期、互斥锁/读写锁/条件变量、atomic 无锁编程、WaitGroup/Once、Channel;
  • 高级并发模式:Context 超时控制、Pipeline/Fan-out/Fan-in/Worker Pool 经典模式、并发陷阱与最佳实践;
  • 工程实战:解析 net/http 优雅关闭、beego 并发启动、sync.Pool 等真实开源框架的并发设计。

每个知识点都从原理出发,辅以可运行的代码示例,最终落脚到工程实践,帮助你真正写出正确、高效的并发代码。

你会学到什么?

  • 理解 Go 并发哲学:搞清楚 CSP 模型与共享内存模型的本质区别,建立 Go 并发编程的正确思维框架;
  • 掌握 GMP 调度机制:深入理解 Goroutine、Machine、Processor 三者协作关系,理解 goroutine 调度的底层逻辑;
  • 吃透 Go 内存模型:理解 Happens-Before 原则,彻底搞清楚什么情况下会出现内存可见性问题;
  • 熟练使用同步原语:sync.Mutex、sync.RWMutex、sync.Cond、sync.WaitGroup、sync.Once、sync/atomic,知道每种工具的适用场景与底层实现;
  • 深度掌握 Channel:理解 channel 的内部结构、发送/接收的阻塞机制、select 多路复用,以及 channel 与 timer 的配合使用;
  • 运用经典并发模式:能够在工程中落地 Pipeline、Fan-out/Fan-in、Worker Pool 等模式,用 Context 优雅控制并发任务的生命周期;
  • 识别并规避并发陷阱:掌握常见并发 Bug 的成因与排查手段,建立并发编程的工程直觉;
  • 读懂框架级并发设计:通过分析 net/http、beego、sync.Pool 等真实源码,将所学知识融会贯通,具备架构级的并发设计能力。

适宜人群

  • 有 Go 语言基础(了解函数、结构体、接口),希望系统学习 Go 并发编程的开发者;
  • 有 Java、Python 等其他语言并发经验,希望理解 Go 并发模型差异的工程师;
  • 渴望深入理解 GMP 调度模型、Go 内存模型等底层机制的中高级 Go 开发者;
  • 在生产环境中踩过并发 Bug,希望建立系统性认知的后端技术人员;
  • 备战大厂面试,需要攻克 Go 并发专题的求职者。

阅读本小册不需要操作系统或编译原理专业背景,但建议具备 Go 基本语法基础。

Share this book

Author

About the Author

Contents

Table of Contents

第 1 章:Go 并发编程概述

  1. 1.1 前言
  2. 1.2 Go 为什么天生适合并发
  3. 1.3 传统并发模型:共享内存
  4. 1.4 Go 的并发哲学:通过通信来共享内存
  5. 1.5 CSP 模型:Go 并发的理论根基
  6. 1.6 站在 goroutine 的视角思考并发
  7. 1.7 Java 与 Go 并发模型的直观对比
  8. 1.8 本书的内容结构
  9. 1.9 总结

第 2 章:并发与并行

  1. 2.1 前言
  2. 2.2 进程与线程的基础概念
  3. 2.3 什么是并发,什么是并行
  4. 2.4 为什么需要多线程并发编程
  5. 2.5 总结

第 3 章:并发编程难在哪里

  1. 3.1 前言
  2. 3.2 数据竞争
  3. 3.3 操作的原子性
  4. 3.4 内存访问同步
  5. 3.5 总结

第 4 章:Go 线程模型——GMP 深度解析

  1. 4.1 前言
  2. 4.2 三种经典线程模型
  3. 4.3 Go 的 GMP 线程模型
  4. 4.4 goroutine 阻塞时的 P 迁移机制
  5. 4.5 总结

第 5 章:Go 内存模型与 Happens-Before 原则

  1. 5.1 前言
  2. 5.2 Happens-Before 原则
  3. 5.3 同步(Synchronization)
  4. 5.4 变量在栈还是堆上分配
  5. 5.5 总结
  6. 5.6 不正确的同步案例

第 6 章:Goroutine 的创建与生命周期管理

  1. 6.1 前言
  2. 6.2 goroutine 是轻量级的线程
  3. 6.3 runtime 包:调度控制
  4. 6.4 如何优雅地终止一个 goroutine
  5. 6.5 goroutine 的 panic 处理与 recover
  6. 6.6 goroutine 泄漏:成因与防范
  7. 6.7 闭包变量捕获的常见陷阱
  8. 6.8 总结

第 7 章:sync 包——互斥锁、读写锁与条件变量

  1. 7.1 前言
  2. 7.2 sync.Mutex:互斥锁
  3. 7.3 sync.RWMutex:读写分离锁
  4. 7.4 sync.Cond:条件变量
  5. 7.5 锁的 happens-before 语义
  6. 7.6 死锁的成因与避免
  7. 7.7 总结

第 8 章:sync/atomic——无锁并发编程

  1. 8.1 前言
  2. 8.2 Add 操作:原子性加减
  3. 8.3 CAS 操作:乐观锁思想
  4. 8.4 Load 和 Store 操作:原子读写
  5. 8.5 Swap 操作:原子替换
  6. 8.6 ABA 问题
  7. 8.7 atomic.Value:任意类型的原子读写
  8. 8.8 总结

第 9 章:sync.WaitGroup 与 sync.Once

  1. 9.1 前言
  2. 9.2 sync.WaitGroup:等待一组 goroutine 完成
  3. 9.3 sync.Once:保证只执行一次
  4. 9.4 实际应用场景
  5. 9.5 总结

第 10 章:Channel——Go 并发的核心通信机制

  1. 10.1 前言
  2. 10.2 channel 的分类与方向
  3. 10.3 无缓冲通道的同步语义
  4. 10.4 有缓冲通道与生产消费模型
  5. 10.5 channel 的关闭规则
  6. 10.6 select:多路 channel 复用
  7. 10.7 channel 的 happens-before 语义
  8. 10.8 channel 与 time 包的配合
  9. 10.9 总结

第 11 章:Context——并发任务的控制与超时

  1. 11.1 前言
  2. 11.2 Context 接口
  3. 11.3 四种 context 类型
  4. 11.4 context 的树形传播模型
  5. 11.5 使用 context 实现超时控制
  6. 11.6 在 HTTP handler 中使用 context
  7. 11.7 context.Value 的正确使用
  8. 11.8 总结

第 12 章:经典并发模式(Pipeline、Fan-out/Fan-in、Worker Pool)

  1. 12.1 前言
  2. 12.2 Pipeline 模式
  3. 12.3 Fan-out 与 Fan-in 模式
  4. 12.4 Worker Pool 模式
  5. 12.6 为并发模式添加 context 取消支持
  6. 12.7 模式的可组合性
  7. 12.8 总结

第 13 章:并发陷阱与最佳实践

  1. 13.1 前言
  2. 13.2 goroutine 泄漏的检测
  3. 13.3 数据竞争与 Race Detector
  4. 13.4 channel 的常见误用
  5. 13.5 锁的常见误用
  6. 13.6 for range 变量捕获陷阱
  7. 13.7 并发安全的 map
  8. 13.8 并发编程最佳实践总结
  9. 13.9 总结

第 14 章:从开源框架看并发设计

  1. 14.1 前言
  2. 14.4 net/http 的优雅关闭与并发模型
  3. 14.5 sync.Pool:对象池与 GC 的协作
  4. 14.6 何时用 channel、锁还是原子操作
  5. 14.7 pprof 并发性能分析
  6. 14.8 生产级并发系统设计检查清单
  7. 14.9 总结

附录

  1. 附录 A:Go 并发工具速查表
  2. 附录 B:常用并发调试命令
  3. 附录 C:推荐阅读

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub