fix(go): 修正 Go 知识库 2 处高危 + 24 处中危,全站版本基准迁到 Go 1.25

审查结论见 go-knowledge-review.md(首轮快照,问题已落地修复)。

正文修正:
- 01 高危:StackInuse 计算的移位优先级 bug(/1<<20 被解析成 (x/1)<<20,编译失败)
- 01:gogo 是 JMP 不是 RET;新 G 先进 runnext(旧 runnext 落队尾);异步抢占走 SIGURG,与序言协作式抢占分开;栈内存由 stackalloc 取 span,不是虚拟地址预留
- 02:61 分支只取 1 个 G(批量走 globrunqgetbatch);"质数防共振"是坊间说法;自旋 M 上限是忙碌 P 的一半;exitsyscallfast 判的是 _Psyscall 而非"P 在空闲链"
- 03:pageAlloc chunk 是 4 MB/512 页(2^21 页是 maxPackedValue);tiny 只对齐块内偏移;allocCache 1=空闲
- 04:STW① 不扫根;work.full 是全局 CAS 栈
- 05 高危:sweepgen 判定表两行对调(sg-2 未扫谁抢谁扫、sg-1 已被认领);lastgc 取 memstats.last_gc_nanotime
- 06:size class 表自 1.11 未变,1.24 变的是 malloc header;cap 0→4 归因 1.25 栈后备数组;累计拷贝约 5n
- 07:map 三代关系(≤1.23 / 1.24-1.25 / 1.26);hashWriting→maps.Map.writing;遍历只保证"删掉且未到达的不再产出"
- 08:缓冲区满时的拷贝方向写反;hchan 12 字段(timer 1.23、bubble 1.25)
- 09:defer 入口补 runtime.Goexit(假 _panic 执行,不是 deferreturn)
- 10:Mutex 三字段 + 1.26 实现搬进 internal/sync;内存模型是独立文档;WaitGroup 三种 panic 拆表 + wg.Go(1.25)
- 11:Ticker 不创建 goroutine;errgroup 示例两闭包同写 order 的 write-write race
- 12:itab.Hash 不用于断言(动态 itab 置 0);PGO 去虚拟化是 1.21
- 13:printABC 死锁真正发生在最后一枚令牌;printNumbers 奇数时可能少打最后一个数
- 14:ConcurrentHashMap 无 Segment;>32KB 才是大对象;源码地图归属修正

版本口径:
- README/SPEC/index 基准改为 Go 1.25,标注格式统一为"1.23 及以前 / 1.24 起 / 1.26 起"
- 标出 1.25 之后废弃项:_Psyscall(1.26)、GOEXPERIMENT=noswissmap(1.26 删除)、synctest 旧实验 API(1.26)
- 补 1.25 事实:GOMAXPROCS 容器感知、sync.WaitGroup.Go、testing/synctest GA、Green Tea GC

校验:check-pages 15 页通过;114 个 go 代码块 109 通过、0 失败、5 个伪代码片段;KPI 14 章/126 节/151 段/9.6 万字。
This commit is contained in:
2026-10-21 15:21:43 +08:00
parent d9629e87a8
commit 33d6090c8d
18 changed files with 554 additions and 217 deletions

View File

@@ -67,19 +67,21 @@
</div>
<h3 id="sec-8-2">8.2 hchan 结构:buf、sendq、recvq 与锁</h3>
<p>channel 在运行时是 <code>runtime.hchan</code>(Go 1.22–1.24 的 <code>runtime/chan.go</code>),字段分三组:
<p>channel 在运行时是 <code>runtime.hchan</code>(Go 1.25 起的 <code>runtime/chan.go</code>),字段分三组:
环形队列状态、元素类型信息、两个等待队列。</p>
<pre data-lang="go" data-name="runtime/chan.go:hchan(Go 1.22–1.24)">type hchan struct {
<pre data-lang="go" data-name="runtime/chan.go:hchan(Go 1.25 起,12 个字段)">type hchan struct {
qcount uint // 当前元素个数
dataqsiz uint // 环形缓冲长度,即 make 时的容量
buf unsafe.Pointer // 指向 dataqsiz 个元素的数组
elemsize uint16 // 元素大小,所以元素不能超过 64 KB
closed uint32 // 0=打开,1=已关闭
timer *timer // 1.23 起:驱动这个 channel 的 timer
elemtype *_type // 元素类型,typedmemmove 与 GC 扫描要用
sendx uint // 下一个写入位置
recvx uint // 下一个读出位置
recvq waitq // 等待接收的 goroutine 队列
sendq waitq // 等待发送的 goroutine 队列
bubble *synctestBubble // 1.25 起:testing/synctest 的 bubble 归属
lock mutex // 保护上面所有字段
}</pre>
@@ -95,8 +97,12 @@
不保证它指向的内容不被并发修改。</p>
<div class="box note">
<span class="bt">字段随版本略有增减</span>
<p>Go 1.23 起 <code>hchan</code> 多了 <code>timer *timer</code>,更晚的版本还有 synctest 相关字段。
面试背这 11 个经典字段就够,遇到「字段顺序」追问,答「以当前版本源码为准」更稳。</p>
<p>Go 1.23 起 <code>hchan</code> 多了 <code>timer *timer</code>;<b>1.25 起</b>又多了
<code>bubble *synctestBubble</code>(<code>testing/synctest</code> 在 1.25 GA),所以现在是
<strong>12 个字段</strong>(外加 <code>lock</code>),1.26 与 1.25 相同。面试按这 12 个答即可;
被追问「为什么顺序是这样」,答「这是<b>源码里的声明顺序</b>,标量字段与指针字段是交错的
(<code>buf</code>、<code>timer</code>、<code>elemtype</code>、<code>recvq</code>、<code>sendq</code>、
<code>bubble</code> 都是指针),照源码抄,别按记忆重排」。</p>
</div>
<h3 id="sec-8-3">8.3 创建:makechan 与容量约束</h3>
@@ -195,7 +201,10 @@
<div class="box note">
<span class="bt">为什么现在不需要「醒来重试」</span>
<p>早期实现是「写缓冲 → 唤醒 → 抢读 → 失败再睡」,靠重试完成交接,可能被后来者插队。
现在接收方已把数据拷进缓冲区、把发送方的值填进槽位,被唤醒的发送方只收到一个「你的值已被取走」的通知,
现在接收方在 <code>recv</code> 里一次做两步:先把 <code>buf[recvx]</code> 拷到自己的接收变量
(<code>typedmemmove(c.elemtype, ep, qp)</code>),再把发送方待发的值直接写进 <code>buf[recvx]</code>
这个槽(<code>typedmemmove(c.elemtype, qp, sg.elem)</code>),随后 <code>recvx</code> 自增回绕并令
<code>sendx = recvx</code>;被唤醒的发送方只收到一个「你的值已被取走」的通知,
一次改动同时修好了 FIFO 语义与惊群。</p>
</div>