Files
materials/knowledge/Go/index.html
huanghaosheng a0f412f3e8 fix(go): 第二轮细查再修正 12 处版本与机制细节
- 02:reentersyscall 在 1.25 的顺序是 save -> 改 _Gsyscall -> 复制 syscalltick、解绑、置 _Psyscall;1.26 起整个解绑取消,P 保持 _Prunning
- 03:pallocSum 的 bit63 是"整片完全空闲"哨兵(三个数都等于 2^21 时只置这一位),不是闲置位
- 04:STW① 的 gcBgMarkStartWorkers 在停世界之前执行(mgc.go:817 -> :834)
- 07:sync.Map 三代关系:1.23 及以前 read/dirty;1.24 起 HashTrieMap 可选(GOEXPERIMENT=synchashtriemap);1.25 起默认;1.26 删旧实现
- 10:Mutex 实现搬到 internal/sync 是 1.24(不是 1.26);WaitGroup waiter 只占低 31 位,bit31 是 synctest bubble 标志;wg.Go 的"panic 不调 Done"是 1.26 行为,1.25 要求 f 不能 panic
- 11:Ticker 不可达可回收始于 1.23(原文把 1.22 及以前写成"1.23 及以前",与同页自相矛盾);cancelCtx.err 是 atomic.Value
- 14:GMP 调度器是 Go 1.1 引入(P / per-P runq / work-stealing),1.5 只是 GOMAXPROCS 默认改为核数

校验:check-pages 15 页通过;114 个 go 代码块 -> 16/16 完整程序实跑通过、5 个伪代码片段预期解析失败;KPI 9.7 万字。
2026-10-21 15:54:40 +08:00

475 lines
49 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-CN" data-theme="light">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Go 八股知识库 · 从运行时原理到面试表达</title>
<meta name="description" content="Go 后端八股专题:14 章覆盖 goroutine 与 GMP、内存分配、垃圾回收、slice/map/channel、defer、锁与同步原语、context、接口、并发编程题与面试速查,每个结论都落到源码、代码输出与面试答法。">
<link rel="stylesheet" href="assets/doc.css">
<style>
/* ===== 目录页专属样式(章节页不使用) ===== */
.hero{background:
radial-gradient(900px 240px at 90% -30%,rgba(0,173,216,.18),transparent),
radial-gradient(700px 220px at 4% -40%,rgba(83,211,234,.16),transparent),
var(--card);
border:1px solid var(--line);border-radius:18px;padding:26px 28px 22px;box-shadow:var(--shadow);margin:0 0 20px}
.hero h1{margin:0 0 8px;font-size:34px;letter-spacing:-.4px;line-height:1.3}
.hero h1 em{font-style:normal;background:linear-gradient(135deg,#00758f,#00add8);-webkit-background-clip:text;
background-clip:text;-webkit-text-fill-color:transparent}
.hero p{margin:0 0 12px;color:var(--muted);font-size:16.6px;max-width:96ch}
.kpis{display:flex;gap:10px;flex-wrap:wrap;margin-top:8px}
.kpi{border:1px solid var(--line);background:var(--soft);border-radius:12px;padding:10px 15px;min-width:120px}
.kpi b{display:block;font-size:23px;font-variant-numeric:tabular-nums;line-height:1.3}
.kpi span{font-size:13.8px;color:var(--muted)}
.toolbar{display:flex;gap:9px;align-items:center;flex-wrap:wrap;margin:16px 0 0;padding-top:14px;border-top:1px dashed var(--line)}
.toolbar .search{position:relative}
.toolbar .search input{font:inherit;font-size:15.6px;padding:9px 13px 9px 34px;border:1px solid var(--line);
border-radius:10px;background:var(--card);color:var(--ink);outline:none;width:360px;max-width:60vw}
.toolbar .search input:focus{border-color:var(--brand);box-shadow:0 0 0 3px var(--brand-soft)}
.toolbar .search::before{content:"⌕";position:absolute;left:12px;top:50%;transform:translateY(-52%);
color:var(--muted);font-size:17px}
.progwrap{display:flex;align-items:center;gap:9px;margin-left:auto}
.progwrap .bar{width:132px;height:7px;border-radius:999px;background:var(--soft);border:1px solid var(--line);overflow:hidden}
.progwrap .bar i{display:block;height:100%;width:0;border-radius:999px;background:linear-gradient(90deg,#00758f,#00add8);transition:width .3s}
.progwrap b{font-size:15px;font-variant-numeric:tabular-nums;min-width:40px;text-align:right}
section.block{margin-top:22px;background:var(--card);border:1px solid var(--line);border-radius:16px;
padding:22px 24px;box-shadow:var(--shadow);scroll-margin-top:80px}
section.block > h2{margin-top:0}
section.block > .sub{margin:-6px 0 16px;font-size:15.6px;color:var(--muted)}
.map{display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:12px}
.map .grp{border:1px solid var(--line);border-radius:13px;padding:14px 16px;background:var(--soft)}
.map .grp h4{margin:0 0 4px;font-size:16.6px}
.map .grp p{margin:0 0 9px;font-size:14.8px;color:var(--muted)}
.map .grp .lk{display:flex;flex-wrap:wrap;gap:6px}
.map .grp a{font-size:14.4px;border:1px solid var(--line);background:var(--card);border-radius:999px;
padding:3px 10px;color:var(--muted);white-space:nowrap}
.map .grp a:hover{border-color:var(--brand);color:var(--brand);text-decoration:none}
.chapcard{border:1px solid var(--line);border-radius:14px;padding:15px 18px;margin-bottom:11px;background:var(--soft);
transition:border-color .15s}
.chapcard:hover{border-color:color-mix(in srgb,var(--brand) 45%,var(--line))}
.chapcard.done-shown{border-color:color-mix(in srgb,var(--ok) 45%,var(--line));
background:color-mix(in srgb,var(--ok) 5%,var(--card))}
.chapcard.hide{display:none}
.cc-head{display:flex;align-items:flex-start;gap:11px}
.cc-head input[type=checkbox]{margin:4px 0 0;width:19px;height:19px;accent-color:var(--ok);cursor:pointer;flex:0 0 auto}
.cc-no{display:inline-grid;place-items:center;min-width:30px;height:30px;padding:0 8px;border-radius:8px;
background:var(--brand-soft);color:var(--brand);font-size:14.4px;font-weight:800;flex:0 0 auto}
.cc-title{font-size:19px;font-weight:700;color:var(--ink);flex:1;min-width:200px}
.cc-title:hover{color:var(--brand);text-decoration:none}
.cc-go{font-size:14.6px;font-weight:700;color:var(--brand);white-space:nowrap;margin-top:2px}
.cc-desc{margin:8px 0 0 41px;font-size:15.6px;color:var(--muted)}
.cc-secs{margin:9px 0 0 41px;font-size:15px;color:var(--muted)}
.cc-secs summary{cursor:pointer;color:var(--brand);font-size:14.6px;font-weight:600}
.cc-secs div{margin-top:8px;line-height:2}
.cc-secs a{color:var(--muted);border-bottom:1px solid var(--line);margin-right:13px;
display:inline-block}
.cc-secs a:hover{color:var(--brand);border-bottom-color:var(--brand);text-decoration:none}
.grid2{display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:12px}
.grid2 .box{margin:0}
.foot{margin-top:26px}
@media (max-width:1080px){
.hero h1{font-size:26px}
.cc-desc,.cc-secs{margin-left:0}
.progwrap{width:100%;margin-left:0}
}
</style>
</head>
<body>
<header class="topbar">
<div class="tb-in">
<a class="brand" href="index.html"><span class="logo">GO</span><div>Go 八股<small>从运行时原理到面试表达</small></div></a>
<div class="spacer"></div>
<button class="btn" id="tocToggle">☰ 章节</button>
<button class="btn" id="themeBtn" title="切换到深色主题">深色</button>
</div>
</header>
<div class="layout" id="layout">
<aside class="sidebar">
<h2>章节目录</h2>
<nav id="chapters"><a class="toc-a" href="01-goroutine.html"><i class="cn">1</i>从进程到 goroutine:并发模型的底层账本</a><a class="toc-a" href="02-gmp.html"><i class="cn">2</i>GMP 调度器:Go 如何把十万个 goroutine 跑起来</a><a class="toc-a" href="03-memory.html"><i class="cn">3</i>内存分配:从 mspan 到 mallocgc</a><a class="toc-a" href="04-gc-mark.html"><i class="cn">4</i>垃圾回收(上):三色标记与写屏障</a><a class="toc-a" href="05-gc-source.html"><i class="cn">5</i>垃圾回收(下):一次 GC 的完整生命周期</a><a class="toc-a" href="06-slice.html"><i class="cn">6</i>slice:视图、扩容与共享底层数组</a><a class="toc-a" href="07-map.html"><i class="cn">7</i>map:从 hmap/bmap 到 1.24 Swiss Table</a><a class="toc-a" href="08-channel.html"><i class="cn">8</i>channel:CSP 的载体与并发编排</a><a class="toc-a" href="09-defer.html"><i class="cn">9</i>defer、panic、recover 与闭包</a><a class="toc-a" href="10-sync.html"><i class="cn">10</i>锁与同步原语:从 Mutex 到 singleflight</a><a class="toc-a" href="11-context.html"><i class="cn">11</i>context:取消、超时与 goroutine 泄露防治</a><a class="toc-a" href="12-interface.html"><i class="cn">12</i>接口:隐式契约与运行时表示</a><a class="toc-a" href="13-puzzles.html"><i class="cn">13</i>并发编程题:从交替打印到 worker pool</a><a class="toc-a" href="14-interview.html"><i class="cn">14</i>面试速查:对比、版本演进与高频追问</a></nav>
<div class="side-foot">
<a class="btn" href="#chapters-list">章节目录</a>
<a class="btn" href="#start">5 分钟上手</a>
<a class="btn" href="#pitfalls">最常见的误区</a>
<a class="btn" href="../../SRE工程师/sre-learning-path/index.html">SRE 学习路径 ↗</a>
</div>
</aside>
<div class="mask" id="mask"></div>
<main class="main-col">
<article>
<section class="hero">
<h1>Go 八股:<em>把「背结论」换成「讲清机制」</em></h1>
<p>这份站点把散落在面经、源码笔记里的 Go 高频题重新组织成一条链:
<b>goroutine 怎么跑 → 内存怎么分 → 垃圾怎么收 → 容器(slice/map/channel)怎么实现 →
同步与取消怎么写对 → 面试怎么答</b>。每个结论都尽量给出源码位置、可运行代码或实测输出,
被追问「为什么」时不至于卡住。</p>
<div class="kpis" id="kpis"><!-- AUTO:kpis -->
<div class="kpi"><b>14</b><span>章 · 完整知识体系</span></div>
<div class="kpi"><b>126</b><span>个知识点小节</span></div>
<div class="kpi"><b>151</b><span>段可复制代码</span></div>
<div class="kpi"><b>9.7 万</b><span>字正文</span></div>
<!-- /AUTO:kpis --></div>
<div class="toolbar">
<div class="search"><input id="q" type="search" placeholder="搜知识点:GMP / 三色标记 / 逃逸分析"></div>
<button class="btn mini" id="expandAll">展开全部小节</button>
<button class="btn mini" id="resetBtn">重置进度</button>
<button class="btn mini" id="printBtn">打印 / 导出 PDF</button>
<div class="progwrap">
<div class="bar"><i id="bar"></i></div>
<b id="pct">0%</b>
</div>
</div>
</section>
<section class="block" id="map">
<h2>知识地图:14 章分别在讲什么</h2>
<p class="sub">按「运行时 → 数据结构 → 并发 → 表达」四层组织。面试前时间紧,可以先读每章末尾的「面试怎么答」,再回头补原理。</p>
<div class="map">
<div class="grp">
<h4>① 并发模型与调度</h4>
<p>进程/线程/协程的账本,goroutine 为什么便宜,GMP 如何在没有全局锁的情况下调度十万个 G。</p>
<div class="lk">
<a href="01-goroutine.html">1 从进程到 goroutine</a>
<a href="02-gmp.html">2 GMP 调度器</a>
</div>
</div>
<div class="grp">
<h4>② 内存与垃圾回收</h4>
<p>对象从 mcache 分配到堆,三色标记怎么在程序继续跑的情况下找全存活对象,GC 的代价花在哪。</p>
<div class="lk">
<a href="03-memory.html">3 内存分配</a>
<a href="04-gc-mark.html">4 三色标记与写屏障</a>
<a href="05-gc-source.html">5 一次 GC 的生命周期</a>
</div>
</div>
<div class="grp">
<h4>③ 核心数据结构</h4>
<p>slice 的视图语义、map 的桶与 Swiss Table、channel 的等待队列——面试最容易被追问实现的三块。</p>
<div class="lk">
<a href="06-slice.html">6 slice</a>
<a href="07-map.html">7 map</a>
<a href="08-channel.html">8 channel</a>
</div>
</div>
<div class="grp">
<h4>④ 语言机制</h4>
<p>defer 的三种实现与返回值修改、panic/recover 的执行链、闭包捕获、接口的 iface/eface 与 nil 陷阱。</p>
<div class="lk">
<a href="09-defer.html">9 defer / panic / 闭包</a>
<a href="12-interface.html">12 接口</a>
</div>
</div>
<div class="grp">
<h4>⑤ 同步与取消</h4>
<p>Mutex 的饥饿模式、atomic 与自旋、Once/WaitGroup/singleflight,以及 context 如何把取消信号传下去。</p>
<div class="lk">
<a href="10-sync.html">10 锁与同步原语</a>
<a href="11-context.html">11 context 与泄露</a>
</div>
</div>
<div class="grp">
<h4>⑥ 动手与表达</h4>
<p>交替打印、生产者消费者、限流、worker pool 的代码模板;对比题、版本演进与 30 条追问的答法。</p>
<div class="lk">
<a href="13-puzzles.html">13 并发编程题</a>
<a href="14-interview.html">14 面试速查</a>
</div>
</div>
</div>
</section>
<section class="block" id="tracks">
<h2>三条阅读线:同一套知识,三种用法</h2>
<p class="sub">按你现在的目标挑一条线;三条线合起来就是「能跑、能讲、能写对」的闭环。</p>
<div class="grid2">
<div class="box note">
<span class="bt">主线 A · 面试速答(1–3 天)</span>
<ol class="steps">
<li>先过 <a href="14-interview.html">第 14 章</a> 的对比表与 30 条追问,知道自己缺哪块。</li>
<li>按缺口读 <a href="02-gmp.html">GMP</a>、<a href="04-gc-mark.html">GC</a>、<a href="07-map.html">map</a>、<a href="08-channel.html">channel</a> 四章。</li>
<li>每章只记「结构 → 流程 → 一个坑」,用自己的话复述一遍。</li>
<li>用 <a href="13-puzzles.html">第 13 章</a> 的题手写代码,避免只会背不会写。</li>
</ol>
</div>
<div class="box tip">
<span class="bt">主线 B · 原理深挖(2 周)</span>
<ol class="steps">
<li>从 <a href="01-goroutine.html">第 1 章</a> 的调度单位开始,建立「用户态调度」的直觉。</li>
<li>顺着 <a href="02-gmp.html">GMP</a> → <a href="03-memory.html">内存分配</a> → <a href="04-gc-mark.html">GC</a> 读运行时主线。</li>
<li>对照 <a href="05-gc-source.html">GC 源码走读</a> 与 Go 源码 <code>runtime/mgc.go</code> 读一遍。</li>
<li>把 <a href="06-slice.html">slice</a>/<a href="07-map.html">map</a>/<a href="08-channel.html">channel</a> 的结构体字段默写出来。</li>
</ol>
</div>
<div class="box warn">
<span class="bt">主线 C · 写对并发(工作中)</span>
<ol class="steps">
<li>先看 <a href="10-sync.html">第 10 章</a> 的选型表:什么时候锁、什么时候 channel。</li>
<li>所有阻塞调用都带上 <a href="11-context.html">context</a>,并给 goroutine 想好退出路径。</li>
<li>用 <a href="13-puzzles.html#sec-13-8">worker pool</a> 控制并发度,不要让 goroutine 无限增长。</li>
<li>上线前用 <code>go test -race</code> 与 pprof 各跑一遍。</li>
</ol>
</div>
<div class="box ok">
<span class="bt">怎么用这个站点</span>
<p>左侧目录会随章节展开;每个章节卡片可以勾选「已读」,进度存在浏览器本地。
搜索框支持按章节标题、摘要和小节名过滤。所有代码块都能一键复制,代码里的版本号以 Go 1.25 为准;
涉及版本差异的地方会明确标注「1.23 及以前 / 1.24 起 / 1.26 起」,1.25 之后被废弃或移除的机制会单独指出。</p>
</div>
</div>
</section>
<section class="block" id="chapters-list">
<h2>章节卡片</h2>
<p class="sub">点标题进入章节;展开「小节」可以直接跳到某个知识点。勾选状态只保存在本机浏览器。</p>
<!-- AUTO:cards -->
<div class="chapcard" data-search="从进程到 goroutine:并发模型的底层账本 从进程、线程、协程三种调度单位讲起,拆开 goroutine 的 2kb 动态栈、newproc 创建与 gogo 切换、gomaxprocs 的真实含义,并给出切换成本与阻塞代价的对比数据。 1.1 三种调度单位:进程、线程、协程 1.2 goroutine 是什么:m:n 调度与 2kb 起步 1.3 动态栈:不够就复制,闲着就收缩 1.4 创建与切换:newproc、gogo 与 goexit 1.5 gomaxprocs:限制的不是 goroutine 数 1.6 切换成本对比:为什么说 goroutine 便宜 1.7 阻塞的代价:谁被卡住了 1.8 工程用法:起 goroutine 之前先想好怎么退出">
<div class="cc-head">
<input type="checkbox" data-ch="1" aria-label="标记第 1 章已读">
<span class="cc-no">1</span>
<a class="cc-title" href="01-goroutine.html">从进程到 goroutine:并发模型的底层账本</a>
<a class="cc-go" href="01-goroutine.html">进入 →</a>
</div>
<p class="cc-desc">从进程、线程、协程三种调度单位讲起,拆开 goroutine 的 2KB 动态栈、newproc 创建与 gogo 切换、GOMAXPROCS 的真实含义,并给出切换成本与阻塞代价的对比数据。</p>
<details class="cc-secs">
<summary>8 个小节 · 展开看知识点</summary>
<div><a href="01-goroutine.html#sec-1-1">1.1 三种调度单位:进程、线程、协程</a><a href="01-goroutine.html#sec-1-2">1.2 goroutine 是什么:M:N 调度与 2KB 起步</a><a href="01-goroutine.html#sec-1-3">1.3 动态栈:不够就复制,闲着就收缩</a><a href="01-goroutine.html#sec-1-4">1.4 创建与切换:newproc、gogo 与 goexit</a><a href="01-goroutine.html#sec-1-5">1.5 GOMAXPROCS:限制的不是 goroutine 数</a><a href="01-goroutine.html#sec-1-6">1.6 切换成本对比:为什么说 goroutine 便宜</a><a href="01-goroutine.html#sec-1-7">1.7 阻塞的代价:谁被卡住了</a><a href="01-goroutine.html#sec-1-8">1.8 工程用法:起 goroutine 之前先想好怎么退出</a></div>
</details>
</div>
<div class="chapcard" data-search="gmp 调度器:go 如何把十万个 goroutine 跑起来 从 gm 为什么不够用讲到 g、m、p、schedt 四个结构体,串起 schedule 到 goexit 的调度循环,讲透 work stealing、系统调用 handoff、异步抢占与 11 个调度场景。 2.1 从 gm 到 gmp:为什么需要 p 2.2 三个结构体:g、m、p 与 schedt 2.3 调度循环:schedule → findrunnable → execute → goexit 2.4 本地队列与 work stealing 2.5 系统调用:reentersyscall、handoff 与 exitsyscall 2.6 阻塞与唤醒:park_m、ready 与 wakep 2.7 抢占:从协作式到基于信号的异步抢占 2.8 十一个调度场景串讲 2.9 本地队列挂 p 还是 m,为什么 2.10 观测调度器:schedtrace 与 pprof">
<div class="cc-head">
<input type="checkbox" data-ch="2" aria-label="标记第 2 章已读">
<span class="cc-no">2</span>
<a class="cc-title" href="02-gmp.html">GMP 调度器:Go 如何把十万个 goroutine 跑起来</a>
<a class="cc-go" href="02-gmp.html">进入 →</a>
</div>
<p class="cc-desc">从 GM 为什么不够用讲到 G、M、P、schedt 四个结构体,串起 schedule 到 goexit 的调度循环,讲透 work stealing、系统调用 handoff、异步抢占与 11 个调度场景。</p>
<details class="cc-secs">
<summary>10 个小节 · 展开看知识点</summary>
<div><a href="02-gmp.html#sec-2-1">2.1 从 GM 到 GMP:为什么需要 P</a><a href="02-gmp.html#sec-2-2">2.2 三个结构体:G、M、P 与 schedt</a><a href="02-gmp.html#sec-2-3">2.3 调度循环:schedule → findRunnable → execute → goexit</a><a href="02-gmp.html#sec-2-4">2.4 本地队列与 work stealing</a><a href="02-gmp.html#sec-2-5">2.5 系统调用:reentersyscall、handoff 与 exitsyscall</a><a href="02-gmp.html#sec-2-6">2.6 阻塞与唤醒:park_m、ready 与 wakep</a><a href="02-gmp.html#sec-2-7">2.7 抢占:从协作式到基于信号的异步抢占</a><a href="02-gmp.html#sec-2-8">2.8 十一个调度场景串讲</a><a href="02-gmp.html#sec-2-9">2.9 本地队列挂 P 还是 M,为什么</a><a href="02-gmp.html#sec-2-10">2.10 观测调度器:schedtrace 与 pprof</a></div>
</details>
</div>
<div class="chapcard" data-search="内存分配:从 mspan 到 mallocgc 覆盖虚拟内存分页、mspan 等四级组件、67 种 size class、mallocgc 分配路径、逃逸分析与 new/make。 3.1 虚拟内存与分页:go 向操作系统要内存的方式 3.2 四级组件:mspan、mcache、mcentral 与 mheap 3.3 spanclass 与 size class:67 种规格怎么来的 3.4 分配主流程:tiny → mcache → mcentral → mheap → os 3.5 大对象、pagealloc 与基数树 3.6 逃逸分析:变量到底分配在栈还是堆 3.7 new 与 make 的区别(含编译器/运行时视角) 3.8 回收路径:span 怎么还给 mheap 和操作系统 3.9 工程用法:预分配、sync.pool 与对象复用">
<div class="cc-head">
<input type="checkbox" data-ch="3" aria-label="标记第 3 章已读">
<span class="cc-no">3</span>
<a class="cc-title" href="03-memory.html">内存分配:从 mspan 到 mallocgc</a>
<a class="cc-go" href="03-memory.html">进入 →</a>
</div>
<p class="cc-desc">覆盖虚拟内存分页、mspan 等四级组件、67 种 size class、mallocgc 分配路径、逃逸分析与 new/make。</p>
<details class="cc-secs">
<summary>9 个小节 · 展开看知识点</summary>
<div><a href="03-memory.html#sec-3-1">3.1 虚拟内存与分页:Go 向操作系统要内存的方式</a><a href="03-memory.html#sec-3-2">3.2 四级组件:mspan、mcache、mcentral 与 mheap</a><a href="03-memory.html#sec-3-3">3.3 spanClass 与 size class:67 种规格怎么来的</a><a href="03-memory.html#sec-3-4">3.4 分配主流程:tiny → mcache → mcentral → mheap → OS</a><a href="03-memory.html#sec-3-5">3.5 大对象、pageAlloc 与基数树</a><a href="03-memory.html#sec-3-6">3.6 逃逸分析:变量到底分配在栈还是堆</a><a href="03-memory.html#sec-3-7">3.7 new 与 make 的区别(含编译器/运行时视角)</a><a href="03-memory.html#sec-3-8">3.8 回收路径:span 怎么还给 mheap 和操作系统</a><a href="03-memory.html#sec-3-9">3.9 工程用法:预分配、sync.Pool 与对象复用</a></div>
</details>
</div>
<div class="chapcard" data-search="垃圾回收(上):三色标记与写屏障 讲清四种回收算法、三色抽象、强弱三色不变式、插入与删除写屏障、go 1.8 混合写屏障与两次 stw,并对比 go 与 java 的 cms/g1/zgc。 4.1 为什么需要 gc:手动管理的两类 bug 4.2 四种基本算法:标记清扫、标记压缩、复制与引用计数 4.3 三色抽象:白、灰、黑与标记过程 4.4 三色不变式:强不变式与弱不变式 4.5 插入写屏障与删除写屏障 4.6 混合写屏障:go 1.8 之后怎么做 4.7 stw 到底停在哪两个时刻 4.8 写屏障如何感知引用的新增与删除(编译期插桩) 4.9 go 与 java 的 gc 对比(cms / g1 / zgc) 4.10 常见误解与面试答法">
<div class="cc-head">
<input type="checkbox" data-ch="4" aria-label="标记第 4 章已读">
<span class="cc-no">4</span>
<a class="cc-title" href="04-gc-mark.html">垃圾回收(上):三色标记与写屏障</a>
<a class="cc-go" href="04-gc-mark.html">进入 →</a>
</div>
<p class="cc-desc">讲清四种回收算法、三色抽象、强弱三色不变式、插入与删除写屏障、Go 1.8 混合写屏障与两次 STW,并对比 Go 与 Java 的 CMS/G1/ZGC。</p>
<details class="cc-secs">
<summary>10 个小节 · 展开看知识点</summary>
<div><a href="04-gc-mark.html#sec-4-1">4.1 为什么需要 GC:手动管理的两类 bug</a><a href="04-gc-mark.html#sec-4-2">4.2 四种基本算法:标记清扫、标记压缩、复制与引用计数</a><a href="04-gc-mark.html#sec-4-3">4.3 三色抽象:白、灰、黑与标记过程</a><a href="04-gc-mark.html#sec-4-4">4.4 三色不变式:强不变式与弱不变式</a><a href="04-gc-mark.html#sec-4-5">4.5 插入写屏障与删除写屏障</a><a href="04-gc-mark.html#sec-4-6">4.6 混合写屏障:Go 1.8 之后怎么做</a><a href="04-gc-mark.html#sec-4-7">4.7 STW 到底停在哪两个时刻</a><a href="04-gc-mark.html#sec-4-8">4.8 写屏障如何感知引用的新增与删除(编译期插桩)</a><a href="04-gc-mark.html#sec-4-9">4.9 Go 与 Java 的 GC 对比(CMS / G1 / ZGC)</a><a href="04-gc-mark.html#sec-4-10">4.10 常见误解与面试答法</a></div>
</details>
</div>
<div class="chapcard" data-search="垃圾回收(下):一次 gc 的完整生命周期 完整拆解一次 gc:三种触发、标记准备与写屏障、并发标记、mutator assist、标记终止、惰性清扫、gogc 阈值、scavenger 与调优。 5.1 触发时机:定时、分配阈值与手动触发 5.2 标记准备:stw、开启写屏障与启动标记 worker 5.3 并发标记:灰队列、scanobject 与置灰置黑 5.4 辅助标记:mutator assist 与 25% cpu 5.5 标记终止:stw、关闭屏障与统计 5.6 清扫:mspan 状态与惰性清扫 5.7 下一轮阈值:gogc、gomemlimit 与内存目标 5.8 系统驻留内存回收:scavenger 怎么把内存还给 os 5.9 观测与调优:gctrace、pprof 与参数取舍">
<div class="cc-head">
<input type="checkbox" data-ch="5" aria-label="标记第 5 章已读">
<span class="cc-no">5</span>
<a class="cc-title" href="05-gc-source.html">垃圾回收(下):一次 GC 的完整生命周期</a>
<a class="cc-go" href="05-gc-source.html">进入 →</a>
</div>
<p class="cc-desc">完整拆解一次 GC:三种触发、标记准备与写屏障、并发标记、mutator assist、标记终止、惰性清扫、GOGC 阈值、scavenger 与调优。</p>
<details class="cc-secs">
<summary>9 个小节 · 展开看知识点</summary>
<div><a href="05-gc-source.html#sec-5-1">5.1 触发时机:定时、分配阈值与手动触发</a><a href="05-gc-source.html#sec-5-2">5.2 标记准备:STW、开启写屏障与启动标记 worker</a><a href="05-gc-source.html#sec-5-3">5.3 并发标记:灰队列、scanobject 与置灰置黑</a><a href="05-gc-source.html#sec-5-4">5.4 辅助标记:mutator assist 与 25% CPU</a><a href="05-gc-source.html#sec-5-5">5.5 标记终止:STW、关闭屏障与统计</a><a href="05-gc-source.html#sec-5-6">5.6 清扫:mspan 状态与惰性清扫</a><a href="05-gc-source.html#sec-5-7">5.7 下一轮阈值:GOGC、GOMEMLIMIT 与内存目标</a><a href="05-gc-source.html#sec-5-8">5.8 系统驻留内存回收:scavenger 怎么把内存还给 OS</a><a href="05-gc-source.html#sec-5-9">5.9 观测与调优:gctrace、pprof 与参数取舍</a></div>
</details>
</div>
<div class="chapcard" data-search="slice:视图、扩容与共享底层数组 拆开 slice 的 24 字节头部,讲清四种初始化、越界与边界检查消除、go 1.18 前后的扩容算法与 size class 取整,以及共享底层数组的覆盖、泄漏、传参三个坑,最后给出并发安全的三种方案与预分配、池化实践。 6.1 数据结构:array、len 与 cap 6.2 四种初始化方式与 nil slice 6.3 访问、切片与边界检查 6.4 append 与扩容:go 1.18 前后的容量算法 6.5 共享底层数组的三个坑:覆盖、泄漏与意外修改 6.6 copy 与三索引切片 6.7 slice 并发安全吗?三种方案 6.8 工程用法:预分配、池化与避免大数组滞留">
<div class="cc-head">
<input type="checkbox" data-ch="6" aria-label="标记第 6 章已读">
<span class="cc-no">6</span>
<a class="cc-title" href="06-slice.html">slice:视图、扩容与共享底层数组</a>
<a class="cc-go" href="06-slice.html">进入 →</a>
</div>
<p class="cc-desc">拆开 slice 的 24 字节头部,讲清四种初始化、越界与边界检查消除、Go 1.18 前后的扩容算法与 size class 取整,以及共享底层数组的覆盖、泄漏、传参三个坑,最后给出并发安全的三种方案与预分配、池化实践。</p>
<details class="cc-secs">
<summary>8 个小节 · 展开看知识点</summary>
<div><a href="06-slice.html#sec-6-1">6.1 数据结构:array、len 与 cap</a><a href="06-slice.html#sec-6-2">6.2 四种初始化方式与 nil slice</a><a href="06-slice.html#sec-6-3">6.3 访问、切片与边界检查</a><a href="06-slice.html#sec-6-4">6.4 append 与扩容:Go 1.18 前后的容量算法</a><a href="06-slice.html#sec-6-5">6.5 共享底层数组的三个坑:覆盖、泄漏与意外修改</a><a href="06-slice.html#sec-6-6">6.6 copy 与三索引切片</a><a href="06-slice.html#sec-6-7">6.7 slice 并发安全吗?三种方案</a><a href="06-slice.html#sec-6-8">6.8 工程用法:预分配、池化与避免大数组滞留</a></div>
</details>
</div>
<div class="chapcard" data-search="map:从 hmap/bmap 到 1.24 swiss table 从哈希函数与冲突解法讲到 hmap/bmap 的读写路径、6.5 装载因子与渐进式扩容,再对比 1.24 swiss table 与并发安全的三种方案。 7.1 哈希表基础:哈希函数、冲突与负载因子 7.2 1.23 及以前:hmap 与 bmap 7.3 读路径:从 hash 到 tophash 7.4 写路径与扩容触发条件 7.5 渐进式扩容:evacuate 怎么做 7.6 遍历为什么是无序的 7.7 go 1.24 的 swiss table 7.8 map 并发安全吗?三种方案 7.9 sync.map 与 map 的区别 7.10 哈希攻击与随机种子">
<div class="cc-head">
<input type="checkbox" data-ch="7" aria-label="标记第 7 章已读">
<span class="cc-no">7</span>
<a class="cc-title" href="07-map.html">map:从 hmap/bmap 到 1.24 Swiss Table</a>
<a class="cc-go" href="07-map.html">进入 →</a>
</div>
<p class="cc-desc">从哈希函数与冲突解法讲到 hmap/bmap 的读写路径、6.5 装载因子与渐进式扩容,再对比 1.24 Swiss Table 与并发安全的三种方案。</p>
<details class="cc-secs">
<summary>10 个小节 · 展开看知识点</summary>
<div><a href="07-map.html#sec-7-1">7.1 哈希表基础:哈希函数、冲突与负载因子</a><a href="07-map.html#sec-7-2">7.2 1.23 及以前:hmap 与 bmap</a><a href="07-map.html#sec-7-3">7.3 读路径:从 hash 到 tophash</a><a href="07-map.html#sec-7-4">7.4 写路径与扩容触发条件</a><a href="07-map.html#sec-7-5">7.5 渐进式扩容:evacuate 怎么做</a><a href="07-map.html#sec-7-6">7.6 遍历为什么是无序的</a><a href="07-map.html#sec-7-7">7.7 Go 1.24 的 Swiss Table</a><a href="07-map.html#sec-7-8">7.8 map 并发安全吗?三种方案</a><a href="07-map.html#sec-7-9">7.9 sync.Map 与 map 的区别</a><a href="07-map.html#sec-7-10">7.10 哈希攻击与随机种子</a></div>
</details>
</div>
<div class="chapcard" data-search="channel:csp 的载体与并发编排 从 csp 与 hchan 结构讲起,拆开 channel 创建、收发三条路径与 close 的唤醒逻辑,讲透 select 的随机与加锁顺序,并给出 0/1/n/信号量/worker pool 的容量设计、常见模式、泄漏排查与 channel/mutex 取舍。 8.1 csp 与 channel 的定位 8.2 hchan 结构:buf、sendq、recvq 与锁 8.3 创建:makechan 与容量约束 8.4 发送的三条路径 8.5 接收的三条路径 8.6 close 的语义与三个 panic 8.7 select 的实现:pollorder 与 lockorder 8.8 容量设计:0、1、n、信号量与 worker pool 8.9 常见模式:超时、取消、扇入扇出与泄漏 8.10 channel 与锁的取舍">
<div class="cc-head">
<input type="checkbox" data-ch="8" aria-label="标记第 8 章已读">
<span class="cc-no">8</span>
<a class="cc-title" href="08-channel.html">channel:CSP 的载体与并发编排</a>
<a class="cc-go" href="08-channel.html">进入 →</a>
</div>
<p class="cc-desc">从 CSP 与 hchan 结构讲起,拆开 channel 创建、收发三条路径与 close 的唤醒逻辑,讲透 select 的随机与加锁顺序,并给出 0/1/N/信号量/worker pool 的容量设计、常见模式、泄漏排查与 channel/mutex 取舍。</p>
<details class="cc-secs">
<summary>10 个小节 · 展开看知识点</summary>
<div><a href="08-channel.html#sec-8-1">8.1 CSP 与 channel 的定位</a><a href="08-channel.html#sec-8-2">8.2 hchan 结构:buf、sendq、recvq 与锁</a><a href="08-channel.html#sec-8-3">8.3 创建:makechan 与容量约束</a><a href="08-channel.html#sec-8-4">8.4 发送的三条路径</a><a href="08-channel.html#sec-8-5">8.5 接收的三条路径</a><a href="08-channel.html#sec-8-6">8.6 close 的语义与三个 panic</a><a href="08-channel.html#sec-8-7">8.7 select 的实现:pollorder 与 lockorder</a><a href="08-channel.html#sec-8-8">8.8 容量设计:0、1、N、信号量与 worker pool</a><a href="08-channel.html#sec-8-9">8.9 常见模式:超时、取消、扇入扇出与泄漏</a><a href="08-channel.html#sec-8-10">8.10 channel 与锁的取舍</a></div>
</details>
</div>
<div class="chapcard" data-search="defer、panic、recover 与闭包 从 defer 的 lifo 语义与参数预计算讲起,拆开堆上、栈上、开放编码三种实现与 deferbits 延迟记录,说清命名返回值修改、panic/recover 展开链路、闭包变量捕获,并给出常见坑与工程用法。 9.1 defer 的语义与执行顺序 9.2 参数预计算:为什么 defer 拿到的是旧值 9.3 三种实现:堆上、栈上与开放编码 9.4 开放编码的启用条件与延迟记录 9.5 defer 如何修改命名返回值 9.6 panic 与 recover 的链路 9.7 闭包:捕获变量还是捕获值 9.8 常见坑与工程用法">
<div class="cc-head">
<input type="checkbox" data-ch="9" aria-label="标记第 9 章已读">
<span class="cc-no">9</span>
<a class="cc-title" href="09-defer.html">defer、panic、recover 与闭包</a>
<a class="cc-go" href="09-defer.html">进入 →</a>
</div>
<p class="cc-desc">从 defer 的 LIFO 语义与参数预计算讲起,拆开堆上、栈上、开放编码三种实现与 deferBits 延迟记录,说清命名返回值修改、panic/recover 展开链路、闭包变量捕获,并给出常见坑与工程用法。</p>
<details class="cc-secs">
<summary>8 个小节 · 展开看知识点</summary>
<div><a href="09-defer.html#sec-9-1">9.1 defer 的语义与执行顺序</a><a href="09-defer.html#sec-9-2">9.2 参数预计算:为什么 defer 拿到的是旧值</a><a href="09-defer.html#sec-9-3">9.3 三种实现:堆上、栈上与开放编码</a><a href="09-defer.html#sec-9-4">9.4 开放编码的启用条件与延迟记录</a><a href="09-defer.html#sec-9-5">9.5 defer 如何修改命名返回值</a><a href="09-defer.html#sec-9-6">9.6 panic 与 recover 的链路</a><a href="09-defer.html#sec-9-7">9.7 闭包:捕获变量还是捕获值</a><a href="09-defer.html#sec-9-8">9.8 常见坑与工程用法</a></div>
</details>
</div>
<div class="chapcard" data-search="锁与同步原语:从 mutex 到 singleflight 把 go 的同步原语放回使用场景:mutex 的状态位与正常/饥饿模式、rwmutex 的写者优先、atomic 与内存序、自旋锁的边界、once、waitgroup、singleflight,以及 redis 与 zookeeper 分布式锁的取舍。 10.1 先分类:数据竞争、竞态条件与工具选择 10.2 mutex:状态位、正常模式与饥饿模式 10.3 rwmutex:读锁、写锁与写者优先 10.4 atomic:原子操作与内存序 10.5 自旋锁:什么时候该自旋 10.6 sync.once:双检查与快路径 10.7 waitgroup:计数器、信号量与复用陷阱 10.8 singleflight:合并并发请求 10.9 分布式锁:redis setnx/redlock 与 zookeeper 10.10 选型表与常见错误">
<div class="cc-head">
<input type="checkbox" data-ch="10" aria-label="标记第 10 章已读">
<span class="cc-no">10</span>
<a class="cc-title" href="10-sync.html">锁与同步原语:从 Mutex 到 singleflight</a>
<a class="cc-go" href="10-sync.html">进入 →</a>
</div>
<p class="cc-desc">把 Go 的同步原语放回使用场景:Mutex 的状态位与正常/饥饿模式、RWMutex 的写者优先、atomic 与内存序、自旋锁的边界、Once、WaitGroup、singleflight,以及 Redis 与 ZooKeeper 分布式锁的取舍。</p>
<details class="cc-secs">
<summary>10 个小节 · 展开看知识点</summary>
<div><a href="10-sync.html#sec-10-1">10.1 先分类:数据竞争、竞态条件与工具选择</a><a href="10-sync.html#sec-10-2">10.2 Mutex:状态位、正常模式与饥饿模式</a><a href="10-sync.html#sec-10-3">10.3 RWMutex:读锁、写锁与写者优先</a><a href="10-sync.html#sec-10-4">10.4 atomic:原子操作与内存序</a><a href="10-sync.html#sec-10-5">10.5 自旋锁:什么时候该自旋</a><a href="10-sync.html#sec-10-6">10.6 sync.Once:双检查与快路径</a><a href="10-sync.html#sec-10-7">10.7 WaitGroup:计数器、信号量与复用陷阱</a><a href="10-sync.html#sec-10-8">10.8 singleflight:合并并发请求</a><a href="10-sync.html#sec-10-9">10.9 分布式锁:Redis SETNX/Redlock 与 ZooKeeper</a><a href="10-sync.html#sec-10-10">10.10 选型表与常见错误</a></div>
</details>
</div>
<div class="chapcard" data-search="context:取消、超时与 goroutine 泄露防治 从 context 接口的四个方法讲到 cancelctx、timerctx、valuectx 的实现,拆解取消信号如何沿树传播、超时预算怎么分配、withvalue 的边界,并给出六种 goroutine 泄露场景与检测手段。 11.1 为什么需要 context 11.2 四个实现:emptyctx、cancelctx、timerctx、valuectx 11.3 取消如何沿树传播 11.4 超时与截止时间 11.5 withvalue 的边界 11.6 使用规范:七条硬规则 11.7 goroutine 泄露的六种场景 11.8 检测与防治 11.9 超时串联实战">
<div class="cc-head">
<input type="checkbox" data-ch="11" aria-label="标记第 11 章已读">
<span class="cc-no">11</span>
<a class="cc-title" href="11-context.html">context:取消、超时与 goroutine 泄露防治</a>
<a class="cc-go" href="11-context.html">进入 →</a>
</div>
<p class="cc-desc">从 context 接口的四个方法讲到 cancelCtx、timerCtx、valueCtx 的实现,拆解取消信号如何沿树传播、超时预算怎么分配、WithValue 的边界,并给出六种 goroutine 泄露场景与检测手段。</p>
<details class="cc-secs">
<summary>9 个小节 · 展开看知识点</summary>
<div><a href="11-context.html#sec-11-1">11.1 为什么需要 context</a><a href="11-context.html#sec-11-2">11.2 四个实现:emptyCtx、cancelCtx、timerCtx、valueCtx</a><a href="11-context.html#sec-11-3">11.3 取消如何沿树传播</a><a href="11-context.html#sec-11-4">11.4 超时与截止时间</a><a href="11-context.html#sec-11-5">11.5 WithValue 的边界</a><a href="11-context.html#sec-11-6">11.6 使用规范:七条硬规则</a><a href="11-context.html#sec-11-7">11.7 goroutine 泄露的六种场景</a><a href="11-context.html#sec-11-8">11.8 检测与防治</a><a href="11-context.html#sec-11-9">11.9 超时串联实战</a></div>
</details>
</div>
<div class="chapcard" data-search="接口:隐式契约与运行时表示 从鸭子类型讲到 iface 与 eface 的内存布局、itab 的生成与缓存、类型断言和 type switch 的编译结果,拆开 nil 接口陷阱、接口调用开销,并说明泛型出现后接口的边界。 12.1 设计哲学:隐式实现换来了什么 12.2 两种运行时表示:iface 与 eface 12.3 赋值与 itab 缓存 12.4 类型断言与 type switch 12.5 nil 接口 vs nil 指针(经典陷阱) 12.6 性能代价:接口调用慢在哪 12.7 泛型与接口:go 1.18 之后怎么选 12.8 工程用法与常见错误">
<div class="cc-head">
<input type="checkbox" data-ch="12" aria-label="标记第 12 章已读">
<span class="cc-no">12</span>
<a class="cc-title" href="12-interface.html">接口:隐式契约与运行时表示</a>
<a class="cc-go" href="12-interface.html">进入 →</a>
</div>
<p class="cc-desc">从鸭子类型讲到 iface 与 eface 的内存布局、itab 的生成与缓存、类型断言和 type switch 的编译结果,拆开 nil 接口陷阱、接口调用开销,并说明泛型出现后接口的边界。</p>
<details class="cc-secs">
<summary>8 个小节 · 展开看知识点</summary>
<div><a href="12-interface.html#sec-12-1">12.1 设计哲学:隐式实现换来了什么</a><a href="12-interface.html#sec-12-2">12.2 两种运行时表示:iface 与 eface</a><a href="12-interface.html#sec-12-3">12.3 赋值与 itab 缓存</a><a href="12-interface.html#sec-12-4">12.4 类型断言与 type switch</a><a href="12-interface.html#sec-12-5">12.5 nil 接口 vs nil 指针(经典陷阱)</a><a href="12-interface.html#sec-12-6">12.6 性能代价:接口调用慢在哪</a><a href="12-interface.html#sec-12-7">12.7 泛型与接口:Go 1.18 之后怎么选</a><a href="12-interface.html#sec-12-8">12.8 工程用法与常见错误</a></div>
</details>
</div>
<div class="chapcard" data-search="并发编程题:从交替打印到 worker pool 把 channel、锁、context 放进具体题目:交替打印 abc、交替打印 1-100、十个 goroutine 顺序打印、生产者消费者、超时控制、并发限流、单例、worker pool 与 errgroup,并给出一套可复用的答题模板。 13.1 交替打印 abc 13.2 交替打印 1-100 13.3 十个 goroutine 顺序打印 1-10 13.4 生产者消费者 13.5 超时控制 13.6 并发限流 13.7 单例与 once 13.8 worker pool 与 errgroup 13.9 答题模板">
<div class="cc-head">
<input type="checkbox" data-ch="13" aria-label="标记第 13 章已读">
<span class="cc-no">13</span>
<a class="cc-title" href="13-puzzles.html">并发编程题:从交替打印到 worker pool</a>
<a class="cc-go" href="13-puzzles.html">进入 →</a>
</div>
<p class="cc-desc">把 channel、锁、context 放进具体题目:交替打印 ABC、交替打印 1-100、十个 goroutine 顺序打印、生产者消费者、超时控制、并发限流、单例、worker pool 与 errgroup,并给出一套可复用的答题模板。</p>
<details class="cc-secs">
<summary>9 个小节 · 展开看知识点</summary>
<div><a href="13-puzzles.html#sec-13-1">13.1 交替打印 ABC</a><a href="13-puzzles.html#sec-13-2">13.2 交替打印 1-100</a><a href="13-puzzles.html#sec-13-3">13.3 十个 goroutine 顺序打印 1-10</a><a href="13-puzzles.html#sec-13-4">13.4 生产者消费者</a><a href="13-puzzles.html#sec-13-5">13.5 超时控制</a><a href="13-puzzles.html#sec-13-6">13.6 并发限流</a><a href="13-puzzles.html#sec-13-7">13.7 单例与 Once</a><a href="13-puzzles.html#sec-13-8">13.8 worker pool 与 errgroup</a><a href="13-puzzles.html#sec-13-9">13.9 答题模板</a></div>
</details>
</div>
<div class="chapcard" data-search="面试速查:对比、版本演进与高频追问 把前十三章压缩成面试现场能用的形态:一张表串起运行时、go 与 java 的六维对比、1.5 到 1.26 的版本演进、三十条高频追问、答题框架、gin 请求流程与源码地图。 14.1 一张表串起运行时 14.2 go vs java:六个维度 14.3 版本演进:为什么答案会过时 14.4 高频追问三十条 14.5 答题框架:四步法 14.6 gin 的请求处理流程 14.7 源码地图:从哪读起 14.8 自测清单">
<div class="cc-head">
<input type="checkbox" data-ch="14" aria-label="标记第 14 章已读">
<span class="cc-no">14</span>
<a class="cc-title" href="14-interview.html">面试速查:对比、版本演进与高频追问</a>
<a class="cc-go" href="14-interview.html">进入 →</a>
</div>
<p class="cc-desc">把前十三章压缩成面试现场能用的形态:一张表串起运行时、Go 与 Java 的六维对比、1.5 到 1.26 的版本演进、三十条高频追问、答题框架、Gin 请求流程与源码地图。</p>
<details class="cc-secs">
<summary>8 个小节 · 展开看知识点</summary>
<div><a href="14-interview.html#sec-14-1">14.1 一张表串起运行时</a><a href="14-interview.html#sec-14-2">14.2 Go vs Java:六个维度</a><a href="14-interview.html#sec-14-3">14.3 版本演进:为什么答案会过时</a><a href="14-interview.html#sec-14-4">14.4 高频追问三十条</a><a href="14-interview.html#sec-14-5">14.5 答题框架:四步法</a><a href="14-interview.html#sec-14-6">14.6 Gin 的请求处理流程</a><a href="14-interview.html#sec-14-7">14.7 源码地图:从哪读起</a><a href="14-interview.html#sec-14-8">14.8 自测清单</a></div>
</details>
</div>
<!-- /AUTO:cards -->
</section>
<section class="block" id="start">
<h2>5 分钟上手:先建立一张正确的图</h2>
<p class="sub">如果只给你 5 分钟,记住下面这条链,比背 50 个结论有用。</p>
<ol class="steps">
<li><b>goroutine 是用户态任务</b>:2KB 栈起步,由 Go 运行时调度到 M(内核线程)上执行,
M 必须持有 P 才能跑 G。<a href="01-goroutine.html#sec-1-2">1.2</a> / <a href="02-gmp.html#sec-2-2">2.2</a></li>
<li><b>内存分配走四级缓存</b>:tiny 对象合并、mcache 无锁、mcentral 加锁、mheap 向 OS 要页;
小对象走 size class,大对象直接走页。<a href="03-memory.html#sec-3-4">3.4</a></li>
<li><b>GC 是并发三色标记</b>:只在标记准备和标记终止短暂 STW,靠写屏障保证「黑色对象不会指向白色对象」。<a href="04-gc-mark.html#sec-4-6">4.6</a></li>
<li><b>slice 是视图,map 是桶数组,channel 是带锁的队列</b>:三者都有「共享底层」带来的坑,
也都有对应的并发安全方案。<a href="06-slice.html#sec-6-5">6.5</a> / <a href="07-map.html#sec-7-8">7.8</a> / <a href="08-channel.html#sec-8-2">8.2</a></li>
<li><b>并发的正确性靠三样东西</b>:锁/原子操作保护共享状态,channel 传递所有权,context 负责取消与超时。<a href="10-sync.html">第 10 章</a> / <a href="11-context.html">第 11 章</a></li>
</ol>
<pre data-lang="bash" data-name="自测:这三条命令的输出能解释吗"># 1) 看调度器每秒做了什么(P、G、runqueue、GC)
$ GODEBUG=schedtrace=1000 ./app
# 2) 看变量到底逃逸到堆还是留在栈上
$ go build -gcflags="-m -m" ./...
# 3) 看一次 GC 的耗时与堆增长
$ GODEBUG=gctrace=1 ./app</pre>
</section>
<section class="block" id="pitfalls">
<h2>最常见的 6 个误区</h2>
<p class="sub">这些说法在面经里流传很广,但只对了一半;每一条都在对应章节里展开。</p>
<div class="tablewrap">
<table>
<thead><tr><th>常见说法</th><th>更准确的说法</th><th>展开</th></tr></thead>
<tbody>
<tr><td>「goroutine 比线程轻,所以可以无限开」</td><td>轻的是栈和切换,不是内存总量;没有退出路径的 goroutine 就是泄露。</td><td><a href="11-context.html#sec-11-7">11.7</a></td></tr>
<tr><td>「GOMAXPROCS 越大并发越高」</td><td>它限制的是同时执行用户代码的 M 数;超过 CPU 配额只会增加切换与调度延迟。</td><td><a href="01-goroutine.html#sec-1-5">1.5</a></td></tr>
<tr><td>「Go 的 GC 没有 STW」</td><td>有,只是很短:标记准备与标记终止各一次,通常在百微秒级。</td><td><a href="04-gc-mark.html#sec-4-7">4.7</a></td></tr>
<tr><td>「map 扩容是一次性把数据搬完」</td><td>渐进式扩容:每次读写顺手搬一两个桶,直到 oldbuckets 搬空。</td><td><a href="07-map.html#sec-7-5">7.5</a></td></tr>
<tr><td>「channel 一定比锁慢」</td><td>channel 适合传递所有权与编排,锁适合保护小临界区;选错场景才是慢的根源。</td><td><a href="08-channel.html#sec-8-10">8.10</a></td></tr>
<tr><td>「defer 一定在堆上分配」</td><td>Go 1.14 起常见场景走开放编码(open-coded),defer 记录直接放在栈帧里。</td><td><a href="09-defer.html#sec-9-3">9.3</a></td></tr>
</tbody>
</table>
</div>
</section>
<nav class="pager">
<a class="next" href="01-goroutine.html"><span class="k">开始阅读 →</span><span class="v">第 1 章 · 从进程到 goroutine:并发模型的底层账本</span></a>
</nav>
<p class="foot">
Go 八股知识库 · 共 14 章 126 个小节 · 151 段代码<br>
<a href="index.html">总目录</a> · <a href="../../SRE工程师/sre-learning-path/index.html">SRE 学习路径</a>
</p>
</article>
</main>
</div>
<button id="toTop" title="回到顶部">↑</button>
<script src="assets/doc.js"></script>
<script src="../../assets/back.js" data-portal-back="knowledge/go" data-portal-label="知识库 › Go 八股知识库"></script>
</body>
</html>