2026-10-21 14:11:30 +08:00
<!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 > 第 12 章 接口:隐式契约与运行时表示 · Go 八股知识库< / title >
< meta name = "description" content = "从鸭子类型讲到 iface 与 eface 的内存布局、itab 的生成与缓存、类型断言和 type switch 的编译结果,拆开 nil 接口陷阱、接口调用开销,并说明泛型出现后接口的边界。" >
< link rel = "stylesheet" href = "assets/doc.css" >
< / 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 = "crumbs" >
< a href = "index.html" > 总目录< / a > < span class = "sep" > /< / span > < b > 第 12 章< / b >
< / div >
< 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 > < span class = "toc-a active" aria-current = "page" > < i class = "cn" > 12< / i > 接口:隐式契约与运行时表示< / span > < div class = "subnav current" > < a class = "toc-a lv3" href = "#sec-12-1" > 12.1 设计哲学:隐式实现换来了什么< / a > < a class = "toc-a lv3" href = "#sec-12-2" > 12.2 两种运行时表示: iface 与 eface< / a > < a class = "toc-a lv3" href = "#sec-12-3" > 12.3 赋值与 itab 缓存< / a > < a class = "toc-a lv3" href = "#sec-12-4" > 12.4 类型断言与 type switch< / a > < a class = "toc-a lv3" href = "#sec-12-5" > 12.5 nil 接口 vs nil 指针(经典陷阱)< / a > < a class = "toc-a lv3" href = "#sec-12-6" > 12.6 性能代价:接口调用慢在哪< / a > < a class = "toc-a lv3" href = "#sec-12-7" > 12.7 泛型与接口: Go 1.18 之后怎么选< / a > < a class = "toc-a lv3" href = "#sec-12-8" > 12.8 工程用法与常见错误< / a > < / div > < 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 = "index.html" > ← 总目录< / a >
< / div >
< / aside >
< div class = "mask" id = "mask" > < / div >
< main class = "main-col" >
< article >
< p class = "chapnav" >
< a href = "index.html" > Go 八股< / a >
< span > ·< / span >
< span > 第 < b class = "pos" > 12< / b > / 14 章< / span >
< / p >
< h2 id = "ch12" > < span class = "n" > 12< / span > 接口:隐式契约与运行时表示< / h2 >
2026-10-22 23:35:36 +08:00
< div class = "box ok" >
< span class = "bt" > 面试直答 · 30 秒版< / span >
< p > 接口是 Go 的隐式契约:一个类型只要实现了接口要求的全部方法,就自动满足这个接口,不需要写 implements。运行时的接口值由两部分组成: 类型信息和数据指针。空接口只有这两样; 非空接口还多一张 itab, 也就是方法表, 赋值时把方法地址填进去, 调用时按索引跳转。所以接口调用比直接调用多一次间接寻址, 也常常把值逼到堆上, 热路径上要留意。最经典的坑是 nil 接口:接口等于 nil 要求类型和数据都是 nil, 把一个 nil 指针赋给接口,这个接口本身并不等于 nil。< / p >
< ul >
< li > 接口是运行时多态,适合解耦和替换;泛型是编译期多态,适合算法和容器。< / li >
< li > 接口尽量小,一到两个方法更好组合,也更少逼着实现方塞空方法。< / li >
< li > 判断接口里是不是 nil, 要么用类型断言取出来判, 要么一开始就别返回 nil 指针。< / li >
< / ul >
< / div >
2026-10-21 14:11:30 +08:00
< div class = "lead" >
2026-10-25 17:16:48 +08:00
< b > 这一章解决什么:< / b > 接口语法一天就能学会,但面试真正问的是三件事:< strong > 一个值赋给接口时发生了什么、nil 接口为什么不是 nil、接口调用比直接调用慢在哪< / strong > 。这一章先讲设计( 隐式实现换来了什么) , 再拆运行时( iface/eface/itab) , 最后落到工程判断: 什么时候用接口、什么时候用泛型、什么时候该躲开接口。
< / div >
< h4 > 读之前:六个词< / h4 >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 词< / th > < th > 一句话< / th > < th > 在哪节< / th > < / tr > < / thead >
< tbody >
< tr > < td > 方法集< / td > < td > 这个类型(或它的指针)能调到的全部方法,决定「谁实现了谁」< / td > < td > 12.3< / td > < / tr >
< tr > < td > 接口值< / td > < td > 两块机器字:类型信息 + 数据指针,一共 16 字节< / td > < td > 12.2< / td > < / tr >
< tr > < td > 方法表( itab) < / td > < td > 每个「接口 × 具体类型」一份,方法地址按接口顺序排成数组< / td > < td > 12.2< / td > < / tr >
< tr > < td > 空接口< / td > < td > 只有类型信息和数据,没有方法表,所以只能先断言< / td > < td > 12.2< / td > < / tr >
< tr > < td > 装箱< / td > < td > 把值搬进接口,通常是拷一份能进堆的东西< / td > < td > 12.3< / td > < / tr >
< tr > < td > 带类型的 nil< / td > < td > 类型非空、数据为空,接口因此不等于 nil< / td > < td > 12.5< / td > < / tr >
< / tbody >
< / table >
< / div >
< div class = "box tip" >
< span class = "bt" > 怎么读这一章< / span >
< p > 每节都是「< b > 现象< / b > → < b > 机制< / b > → < b > 代价< / b > → < b > 一句话< / b > 」。现象都能自己跑出来, 或者从编译产物、benchmark 里看到,先看它;标了 < span class = "tag s" > 可跳过< / span > 的段落是字段级、版本级细节,第一遍略过不影响主线;每节最后加粗那句是要背的。< b > 三张图就是全章骨架< / b > :接口值怎么摆、带类型的 nil 差在哪、接口调用多了哪几跳。< / p >
2026-10-21 14:11:30 +08:00
< / div >
< h3 id = "sec-12-1" > 12.1 设计哲学:隐式实现换来了什么< / h3 >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 现象< / span > 把方法名 < code > Read< / code > 故意拼成 < code > Reed< / code > , < code > go build< / code > 一声不响就过了;直到把这个类型赋给 < code > io.Reader< / code > 的那一行, 编译器才翻脸, 报「missing method Read」。< b > 类型自己不知道它实现了谁< / b > ,检查只发生在赋值那一刻。< / p >
< pre data-lang = "go" data-name = "main.go: 拼错方法名, 报错点在赋值处" > type myFile struct{}
func (myFile) Reed(p []byte) (int, error) { return 0, nil }
var _ io.Reader = myFile{}
// cannot use myFile{} ... as io.Reader value in variable declaration:
// myFile does not implement io.Reader (missing method Read)< / pre >
< p > < span class = "tag" > 机制< / span > 编译器只在「需要一个接口」的位置做检查:赋值、传参、返回。它拿具体类型的方法集去和接口的方法集逐个比,名字、签名、导出性都要对上,少一个就报错。< b > 方法集不是「你写了哪些方法」,而是「这个类型的值能调到哪些方法」< / b > ——这个区别在 12.3 会咬人。< / p >
< p > 因为检查发生在使用处,接口就可以由使用方来定义:实现方不用 import 使用方,甚至不用知道这个接口存在。< b > 依赖箭头的方向因此反了过来< / b > 。< / p >
< pre data-lang = "go" data-name = "使用方定义小接口(实现方无需知道)" > // consumer 包:只声明自己用得到的那一个方法
2026-10-21 14:11:30 +08:00
type UserReader interface {
GetUser(ctx context.Context, id int64) (*User, error)
}
func New(users UserReader) *Service { return & Service{users: users} }< / pre >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 代价< / span > 编译器不会提醒你「这个类型本来想实现那个接口」:改名、改签名,报错点常离改动点很远;类型定义上也看不出它实现了谁,只能靠 IDE 或搜赋值点。所以工程上补一行编译期断言,把「我以为它实现了」变成编译错误——不产生任何运行时代码,接口一改它先炸。< / p >
< pre data-lang = "go" data-name = "编译期接口断言" > var _ UserReader = (*UserRepo)(nil) // 不满足时编译失败,零运行时开销< / pre >
2026-10-21 14:11:30 +08:00
< div class = "box tip" >
< span class = "bt" > 接口设计的经验法则< / span >
2026-10-25 17:16:48 +08:00
< p > 接口越小越好,一到三个方法最舒服,标准库的 < code > io.Reader< / code > 、< code > io.Writer< / code > 、< code > fmt.Stringer< / code > 都是这个尺度。大接口应该由小接口组合出来,而不是一次定义十个方法。< / p >
2026-10-21 14:11:30 +08:00
< / div >
2026-10-25 17:16:48 +08:00
< p class = "take" > < b > 一句话:< / b > 隐式实现把检查推到赋值那一刻,换来「接口由使用方定义」;代价是错误发现得晚、实现了谁看不出来,靠编译期断言兜住。< / p >
2026-10-21 14:11:30 +08:00
< h3 id = "sec-12-2" > 12.2 两种运行时表示: iface 与 eface< / h3 >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 现象< / span > 打印两种接口值的大小,都是 < b > 16< / b > 字节;可一个能直接调方法,另一个只能先断言。再用 < code > go build -gcflags=-S< / code > 看一眼:非空接口的第一个字被当成方法表用,空接口的第一个字被当成类型用——< b > 大小一样,含义不同< / b > 。< / p >
< p > < span class = "tag" > 机制< / span > 接口变量不是「对象引用」,它就是两个字:第一块是类型信息,第二块是数据。带方法的接口叫 iface, 第一块指向方法表; 空接口叫 eface, 第一块直接指向具体类型。两个结构体都只有这两个字段, 定义在 < code > runtime/runtime2.go< / code > 。< / p >
< pre data-lang = "go" data-name = "runtime/runtime2.go: 两种接口值( amd64 上各 16 字节)" > type iface struct { // 非空接口:第一个字是方法表
tab *itab
data unsafe.Pointer
2026-10-21 14:11:30 +08:00
}
2026-10-25 17:16:48 +08:00
type eface struct { // 空接口:第一个字就是具体类型
_type *_type
2026-10-21 14:11:30 +08:00
data unsafe.Pointer
}< / pre >
2026-10-25 17:16:48 +08:00
< figure class = "figure" >
< svg viewBox = "0 0 720 400" role = "img" aria-label = "接口值是两块机器字:第一块指向 itab, itab 里按接口方法顺序存着函数地址;第二块指向堆上的数据副本" >
< defs >
< marker id = "ar12a" markerWidth = "9" markerHeight = "9" refX = "7" refY = "3" orient = "auto" >
< path d = "M0,0 L7,3 L0,6 Z" style = "fill:var(--brand)" / >
< / marker >
< / defs >
< text x = "24" y = "22" font-size = "12.5" style = "fill:var(--muted)" > 一个非空接口值: AMD64 上两个机器字, 16 字节< / text >
< rect x = "24" y = "36" width = "220" height = "50" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< line x1 = "134" y1 = "36" x2 = "134" y2 = "86" style = "stroke:var(--line)" / >
< text x = "79" y = "66" text-anchor = "middle" font-size = "13" font-weight = "700" style = "fill:var(--brand-ink)" > tab< / text >
< text x = "189" y = "66" text-anchor = "middle" font-size = "13" font-weight = "700" style = "fill:var(--ink)" > data< / text >
< rect x = "300" y = "30" width = "252" height = "176" rx = "9" style = "fill:var(--card);stroke:var(--brand)" / >
< text x = "316" y = "52" font-size = "12.5" font-weight = "700" style = "fill:var(--brand-ink)" > itab( 每个「接口 × 类型」一份)< / text >
< text x = "316" y = "78" font-size = "12" style = "fill:var(--ink)" > inter → 接口类型 io.ReadWriter< / text >
< text x = "316" y = "100" font-size = "12" style = "fill:var(--ink)" > type → 具体类型 *bytes.Buffer< / text >
< text x = "316" y = "122" font-size = "12" style = "fill:var(--ink)" > hash → 类型哈希( type switch 用)< / text >
< text x = "316" y = "144" font-size = "12" style = "fill:var(--ink)" > fun[0] → Read 的入口< / text >
< text x = "316" y = "166" font-size = "12" style = "fill:var(--ink)" > fun[1] → Write 的入口< / text >
< text x = "316" y = "192" font-size = "11.5" style = "fill:var(--muted)" > 方法按接口定义的顺序排,调用取 fun[i]< / text >
< path d = "M244,61 L292,61" style = "fill:none;stroke:var(--brand);stroke-width:1.6" marker-end = "url(#ar12a)" / >
< rect x = "300" y = "244" width = "260" height = "82" rx = "8" style = "fill:var(--soft);stroke:var(--line);stroke-dasharray:5 4" / >
< text x = "316" y = "270" font-size = "12.5" font-weight = "700" style = "fill:var(--ink)" > 空接口 any( eface) < / text >
< text x = "316" y = "294" font-size = "11.5" style = "fill:var(--muted)" > 第一个字直接是具体类型,没有函数数组< / text >
< text x = "316" y = "314" font-size = "11.5" style = "fill:var(--muted)" > 所以空接口只能先断言,再取里面的值< / text >
< rect x = "24" y = "250" width = "260" height = "68" rx = "9" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "40" y = "278" font-size = "12.5" style = "fill:var(--ink)" > 堆上的 *bytes.Buffer 副本< / text >
< text x = "40" y = "300" font-size = "11.5" style = "fill:var(--muted)" > 装箱是拷贝:改原变量,这里不变< / text >
< path d = "M189,86 L189,242" style = "fill:none;stroke:var(--brand);stroke-width:1.6" marker-end = "url(#ar12a)" / >
< text x = "24" y = "356" font-size = "12.5" style = "fill:var(--muted)" > 判 nil 要求两块都为空;调方法只用 tab, 不解引用 data 就碰不到对象< / text >
< text x = "24" y = "380" font-size = "12.5" style = "fill:var(--muted)" > 同一个具体值装进不同接口,只是 tab 指向了另一份 itab< / text >
< / svg >
< div class = "figcap" > < b > 图 1< / b > · 接口值就是「方法表 + 数据」两块字,方法按接口定义顺序排进 fun 数组< / div >
< / figure >
< p > 方法表是这套设计的核心:每个「接口 × 具体类型」组合只有一份,里面记着接口类型、具体类型、一个给 type switch 用的哈希,以及一个按接口方法顺序排好的函数地址数组。< b > 调用第 i 个方法就是取第 i 项跳过去< / b > ,没有按名字查找这回事。< / p >
2026-10-21 14:11:30 +08:00
< div class = "tablewrap" >
< table >
2026-10-25 17:16:48 +08:00
< thead > < tr > < th > 维度< / th > < th > 非空接口( iface) < / th > < th > 空接口( eface) < / th > < / tr > < / thead >
2026-10-21 14:11:30 +08:00
< tbody >
< tr > < td > 典型类型< / td > < td > < code > io.Reader< / code > 、< code > error< / code > < / td > < td > < code > any< / code > 、< code > interface{}< / code > < / td > < / tr >
2026-10-25 17:16:48 +08:00
< tr > < td > 第一个字< / td > < td > < code > itab< / code > :接口类型 + 具体类型 + 方法表< / td > < td > < code > _type< / code > :只有具体类型< / td > < / tr >
< tr > < td > 方法调用< / td > < td > 取 fun 数组第 i 项间接调用< / td > < td > 无方法可调,先断言< / td > < / tr >
< tr > < td > 赋值成本< / td > < td > 查或建方法表,可能装箱分配< / td > < td > 只装箱,可能分配< / td > < / tr >
2026-10-21 14:11:30 +08:00
< tr > < td > 相等比较< / td > < td > 类型与数据都比较< / td > < td > 同左,但不可比较类型会 panic< / td > < / tr >
< / tbody >
< / table >
< / div >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag s" > 可跳过< / span > 函数数组声明成长度 1、实际按方法数扩展, 是 Go 里常见的「柔性数组」写法;注释还约定 < code > fun[0]< / code > 为 0 就表示这个类型没实现该接口(< code > internal/abi/iface.go:18< / code > )。哈希字段只对编译器静态生成的那份有意义,运行时临时建的一律置 0。< / p >
< p > < span class = "tag" > 代价< / span > 两个机器字是固定成本:装一个 < code > bool< / code > 也要 16 字节,装一个指针也一样——< b > 接口值不会因为里面东西小而变小< / b > 。更贵的是两块字分离: 方法表在只读内存, 数据在堆上, 一次调用要摸两处, 缓存不友好( 12.6 有实测)。< / p >
< p class = "take" > < b > 一句话:< / b > 接口值永远是两块机器字:非空接口第一块是方法表、空接口第一块是类型,第二块都是数据;调用只按方法在接口里的位置取地址,从不按名字查。< / p >
2026-10-21 14:11:30 +08:00
< h3 id = "sec-12-3" > 12.3 赋值与 itab 缓存< / h3 >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 现象< / span > 把不同类型的值装进 < code > any< / code > ,跑一遍 benchmem: 装指针 < b > 0 B/op< / b > ,装 0– 255 的整数也是 < b > 0 B/op< / b > ,装字符串却要 < b > 16 B/op< / b > 加一次分配,装两字的结构体同样 16 B。< b > 同一个「装箱」动作,成本差一个数量级。< / b > < / p >
< p > < span class = "tag" > 机制< / span > 一次赋值分两步:先装箱(把值放进数据字),再拿方法表。< b > 装箱要决定数据字指向哪< / b > : 指针直接存; 太大、或含指针的值在堆上拷一份; 0– 255 的整数指向运行时的一张只读表,连分配都省了。< / p >
< pre data-lang = "go" data-name = "runtime/iface.go: 0– 255 的整数走只读表" > // 装箱整数时的一个分支( convT64 附近)
if val < 256 {
x = & staticuint64s[val] // 零分配,指向只读表
}< / pre >
2026-10-21 14:11:30 +08:00
< div class = "tablewrap" >
< table >
2026-10-25 17:16:48 +08:00
< thead > < tr > < th > 赋给接口的值< / th > < th > 数据字指向< / th > < th > 实测( Go 1.26, amd64) < / th > < / tr > < / thead >
2026-10-21 14:11:30 +08:00
< tbody >
2026-10-25 17:16:48 +08:00
< tr > < td > 指针< / td > < td > 指针本身< / td > < td > 0 B, 0 次分配< / td > < / tr >
< tr > < td > 0– 255 的整数< / td > < td > 只读静态表< / td > < td > 0 B, 0 次分配< / td > < / tr >
< tr > < td > 其它整数、浮点< / td > < td > 堆上副本< / td > < td > 8 B, 1 次分配< / td > < / tr >
< tr > < td > 字符串(两个字)< / td > < td > 堆上副本< / td > < td > 16 B, 1 次分配< / td > < / tr >
< tr > < td > 两字的普通结构体< / td > < td > 堆上副本< / td > < td > 16 B, 1 次分配< / td > < / tr >
2026-10-21 14:11:30 +08:00
< / tbody >
< / table >
< / div >
2026-10-25 17:16:48 +08:00
< p > 这张表要跟第 < a href = "03-memory.html#sec-3-6" > 3.6 节「装箱是否逃逸」< / a > 的口径对齐:< b > 「装进接口就一定逃逸」是错的,「装进接口通常要分配」是对的< / b > 。判据始终是地址会不会活过栈帧;装箱常要分配,是因为得把值挪到 GC 看得见的地方。指针和 0– 255 的整数能免掉这份开销,别把特例当通例。< / p >
< p > 第二步拿方法表,走的是运行时一张全局表:先无锁查(绝大多数命中),没有再加锁重查,仍没有才逐方法比对、建一份新的插进去。所以同一个「接口 × 类型」组合只有第一次赋值付建表成本,之后每次都是查表命中。< / p >
< p > 而装箱是拷贝,这一点会静默改语义:< / p >
2026-10-21 14:11:30 +08:00
< pre data-lang = "go" data-name = "装箱是拷贝" > type Point struct{ X, Y int }
func (p Point) String() string { return fmt.Sprintf("(%d,%d)", p.X, p.Y) }
p := Point{1, 2}
var s fmt.Stringer = p // 拷贝一份进接口
p.X = 100
fmt.Println(s) // (1,2),不是 (100,2)< / pre >
2026-10-25 17:16:48 +08:00
< p > 想通过接口改变原对象,只能装指针;装值就永远隔着这一层副本。< / p >
< p > < span class = "tag s" > 可跳过< / span > 全局方法表用二次探测,容量翻倍增长、永不收缩,建出来的记录放在不受 GC 管理的内存里。< b > 失败也会被缓存< / b > :赋值失败时同样插一条「缺方法」的记录,下次直接命中,只有要 panic 报错名时才重扫一遍方法集。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 方法集按接收者分,值接收者改不了自己< / span >
< p > 把 < code > T{}< / code > 装进接口,接口里躺的是 T 的副本;值接收者的方法改的是这份副本,调用完外面一点没变——实测 < code > c.n< / code > 还是 0。反过来, 方法写成指针接收者时 T 根本不在方法集里,赋值处直接编译失败:< code > counter does not implement CtrP (method AddP has pointer receiver)< / code > 。< b > 要能被接口改变状态,实现方就用指针接收者。< / b > < / p >
< / div >
< p > < span class = "tag" > 代价< / span > 装箱是拷贝,改原变量影响不到接口里的值,排查时极其反直觉;每次装箱还可能给 GC 添一份活对象,热循环里往 < code > any< / code > 里塞整数,分配和回收都会变成 profile 上的常数项。< / p >
< p class = "take" > < b > 一句话:< / b > 赋值就是「装箱 + 查方法表」:装箱决定数据字指向哪(拷贝、只读表还是原指针),查表命中一次就永久复用;装进去的是拷贝。< / p >
2026-10-21 14:11:30 +08:00
< h3 id = "sec-12-4" > 12.4 类型断言与 type switch< / h3 >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 现象< / span > 同一个断言,两种代价。把 < code > i.(string)< / code > 编出来,函数体只有 51 字节,核心一条 < code > CMPQ< / code > :比的是一次指针。换成 < code > i.(io.Reader)< / code > ,编译器在旁边生成一份静态方法表,先比一次,不中再调 < code > runtime.typeAssert< / code > 。< / p >
< p > < span class = "tag" > 机制< / span > 断言问的问题不同,走的路也不同。问「是不是这个具体类型」:空接口比类型指针,非空接口比方法表里的类型字段,都是一次比较。问「是不是某个接口」:要拿到这个组合的方法表,所以是查表甚至建表;失败时按带不带 < code > ok< / code > 决定返回零值还是 panic。< / p >
2026-10-21 14:11:30 +08:00
< pre data-lang = "go" data-name = "四种断言形态" > var i any = "hello"
2026-10-25 17:16:48 +08:00
s, ok := i.(string) // 到具体类型:一次指针比较
2026-10-21 14:11:30 +08:00
if !ok { /* ... */ }
2026-10-25 17:16:48 +08:00
r, ok := i.(io.Reader) // 到接口:查/建方法表
2026-10-21 14:11:30 +08:00
_ = r
v := i.(int) // 失败直接 panic: interface conversion
switch x := i.(type) { // type switch
case string:
fmt.Println("string", x)
case io.Reader:
fmt.Println("reader", x)
default:
fmt.Println("other")
}< / pre >
2026-10-25 17:16:48 +08:00
< p > type switch 没有魔法:编译器把它展平成几个分支,每个分支要么比类型指针、要么查方法表;分支多时还会用类型哈希做跳转优化,所以它和手写一串断言基本等价,可读性却好得多。< / p >
< p > < span class = "tag s" > 可跳过< / span > 方法表里的哈希字段只在编译器静态生成的那份上有效(< code > internal/abi/iface.go:17< / code > 的注释:供 type switch 用),运行时动态建的一律置 0( < code > runtime/iface.go:80-85< / code > )。所以它既不能拿来给断言做快速失败,也不是那张全局表的哈希。< / p >
2026-10-21 14:11:30 +08:00
< div class = "box warn" >
2026-10-25 17:16:48 +08:00
< span class = "bt" > 易错点 · 错误被包装过一层,断言就看不穿< / span >
< p > < code > err.(*MyErr)< / code > 只看最外面这层。一旦用 < code > fmt.Errorf("...: %w", err)< / code > 包过,动态类型就变成 < code > *fmt.wrapError< / code > ,断言直接返回 false; 实测 < code > errors.As< / code > 才是 true。< b > 要按类型取错误,一律 < code > errors.As< / code > ;拿 < code > ==< / code > 比 error 也一样会失效。< / b > < / p >
2026-10-21 14:11:30 +08:00
< / div >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 代价< / span > 断言把类型判断搬到运行时:目标类型写错编译照样通过,测试没覆盖到就等着线上 panic。带 < code > ok< / code > 的写法把失败变成静默零值,目标类型写错时更隐蔽。所以断言密集的地方,通常说明接口该重新设计了。< / p >
< p class = "take" > < b > 一句话:< / b > 断言到具体类型是一次指针比较,断言到接口是查方法表;被包装过的错误只能靠 < code > errors.As< / code > 拆,别拿断言硬碰。< / p >
2026-10-21 14:11:30 +08:00
< h3 id = "sec-12-5" > 12.5 nil 接口 vs nil 指针(经典陷阱)< / h3 >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 现象< / span > 一个函数声明返回 < code > error< / code > ,一路没出错,调用方 < code > if err != nil< / code > 却成立;打印出来是 < code > < nil> < / code > , < code > reflect.TypeOf< / code > 却告诉你类型是 < code > *main.MyErr< / code > 。< b > 「非 nil 的接口,装着 nil 的指针」< / b > ——全章最贵的一个坑。< / p >
< pre data-lang = "go" data-name = "main.go: 反例与正解( 可直接跑) " > type MyErr struct{ msg string }
2026-10-21 14:11:30 +08:00
func (e *MyErr) Error() string { return e.msg }
2026-10-25 17:16:48 +08:00
// 反例:失败才赋值,但最后统一 return e
func badFetch(fail bool) error {
2026-10-21 14:11:30 +08:00
var e *MyErr
2026-10-25 17:16:48 +08:00
if fail {
e = & MyErr{msg: "boom"}
2026-10-21 14:11:30 +08:00
}
return e
}
2026-10-25 17:16:48 +08:00
// 正解:没有错误就走字面 nil
func goodFetch(fail bool) error {
if fail {
return & MyErr{msg: "boom"}
}
return nil
2026-10-21 14:11:30 +08:00
}< / pre >
2026-10-25 17:16:48 +08:00
< pre data-lang = "text" data-name = "上面这段的真实输出" > bad(false): err == nil = false | 打印 < nil> | TypeOf *main.MyErr
bad(true) : err == nil = false | 打印 boom
good(false): err == nil = true
调 bad(false).Error(): panic: invalid memory address or nil pointer dereference< / pre >
< p > < span class = "tag" > 机制< / span > 判 nil 要求两个字都为空。返回类型为 < code > *MyErr< / code > 、值为 nil 的指针时,装箱只填了第一块:方法表非空,数据字是 nil。于是「类型在、数据不在」, 接口整体不等于 nil; 调它的方法能进去, 但接收者是 nil, 一碰字段就崩。< / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 320" role = "img" aria-label = "两个接口值只有 tab 一个非空一个为空,判 nil 的结论完全相反" >
< defs >
< marker id = "ar12b" markerWidth = "9" markerHeight = "9" refX = "7" refY = "3" orient = "auto" >
< path d = "M0,0 L7,3 L0,6 Z" style = "fill:var(--muted)" / >
< / marker >
< / defs >
< text x = "24" y = "24" font-size = "12.5" font-weight = "700" style = "fill:var(--ok)" > ① var err error: 从没装过值< / text >
< rect x = "24" y = "36" width = "300" height = "54" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< line x1 = "174" y1 = "36" x2 = "174" y2 = "90" style = "stroke:var(--line)" / >
< text x = "99" y = "68" text-anchor = "middle" font-size = "12.5" style = "fill:var(--muted)" > tab = nil< / text >
< text x = "249" y = "68" text-anchor = "middle" font-size = "12.5" style = "fill:var(--muted)" > data = nil< / text >
< text x = "396" y = "24" font-size = "12.5" font-weight = "700" style = "fill:var(--warn)" > ② err = (*MyErr)(nil):装了空指针< / text >
< rect x = "396" y = "36" width = "150" height = "54" rx = "8" style = "fill:var(--brand-soft);stroke:var(--brand)" / >
< text x = "471" y = "68" text-anchor = "middle" font-size = "12.5" style = "fill:var(--brand-ink)" > tab ≠ nil< / text >
< rect x = "546" y = "36" width = "150" height = "54" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "621" y = "68" text-anchor = "middle" font-size = "12.5" style = "fill:var(--muted)" > data = nil< / text >
< text x = "24" y = "120" font-size = "12.5" style = "fill:var(--muted)" > 接口判 nil 要求两块都为空 → ① 为真,② 为假< / text >
< rect x = "24" y = "140" width = "300" height = "96" rx = "9" style = "fill:var(--ok-bg);stroke:var(--ok-line)" / >
< text x = "44" y = "170" font-size = "13" font-weight = "700" style = "fill:var(--ok)" > err == nil → true< / text >
< text x = "44" y = "196" font-size = "12" style = "fill:var(--ink)" > 走正常分支,什么都不打< / text >
< text x = "44" y = "218" font-size = "12" style = "fill:var(--muted)" > 调用方以为一切顺利< / text >
< rect x = "396" y = "140" width = "300" height = "96" rx = "9" style = "fill:var(--warn-bg);stroke:var(--warn-line)" / >
< text x = "416" y = "170" font-size = "13" font-weight = "700" style = "fill:var(--warn)" > err == nil → false< / text >
< text x = "416" y = "196" font-size = "12" style = "fill:var(--ink)" > 进错误分支,日志打印 < nil> < / text >
< text x = "416" y = "218" font-size = "12" style = "fill:var(--muted)" > 一调 Error() 就 nil 解引用< / text >
< path d = "M174,90 L174,136" style = "fill:none;stroke:var(--muted);stroke-width:1.5" marker-end = "url(#ar12b)" / >
< path d = "M546,90 L546,136" style = "fill:none;stroke:var(--muted);stroke-width:1.5" marker-end = "url(#ar12b)" / >
< text x = "24" y = "270" font-size = "12.5" style = "fill:var(--muted)" > 正解:没有错误就 return nil, 不要 return 一个值为 nil 的指针< / text >
< text x = "24" y = "294" font-size = "12.5" style = "fill:var(--muted)" > 或者让函数直接返回具体类型,别在接口里兜圈< / text >
< / svg >
< div class = "figcap" > < b > 图 2< / b > · 两个接口值只差 tab 一个字,判 nil 的结论就完全相反< / div >
< / figure >
< p > < span class = "tag" > 代价< / span > 这个 bug 本地很难复现:只有「真的没出错」那条路径才触发,打印出来又是 < code > < nil> < / code > ,看着人畜无害。它会一路穿过 service 和中间件,直到某个 < code > if err != nil { return err }< / code > 把假错误变成 500。< / p >
< div class = "box warn" >
< span class = "bt" > 易错点 · 返回 nil 指针 ≠ 返回 nil 接口< / span >
< p > 函数签名一旦写成返回接口,< b > 每条 return 都要想清楚返回的是接口还是具体指针< / b > 。把 < code > var e *MyErr< / code > 直接 < code > return e< / code > ,等于返回一个「有类型的空值」。修法只有两条:没有错误就 < code > return nil< / code > ,或者让函数别返回接口类型。< / p >
2026-10-21 14:11:30 +08:00
< / div >
2026-10-25 17:16:48 +08:00
< p class = "take" > < b > 一句话:< / b > 接口等于 nil 要求类型和数据都为空;把值为 nil 的具体指针装进接口,得到的不是 nil, 而是「一个装着空指针的非 nil 接口」。< / p >
2026-10-21 14:11:30 +08:00
< h3 id = "sec-12-6" > 12.6 性能代价:接口调用慢在哪< / h3 >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 现象< / span > 同一个方法,直接调和过接口调,本机 benchmark 差 < b > 5 倍< / b > : 0.99 ns/op 对 5.1 ns/op; 一次装箱要 8– 16 字节、十几到几十纳秒,又是十倍以上。绝对值都不大,但在每秒百万次的热点路径上,这五倍会变成真金白银。< / p >
< p > < span class = "tag" > 机制< / span > 看汇编最直接。直接调用编出来是 < code > CALL main.(*buffer).Read(SB)< / code > ,地址写死在指令里;接口调用是 < code > MOVQ 24(AX), DX< / code > 再 < code > CALL DX< / code > ,先从方法表第 3 个槽读出地址,再间接跳过去。< b > 多的是访存和间接跳转,不是查找。< / b > < / p >
< figure class = "figure" >
< svg viewBox = "0 0 720 260" role = "img" aria-label = "直接调用的目标地址写死在指令里;接口调用要先从内存取出函数地址再间接跳转" >
< defs >
< marker id = "ar12c" markerWidth = "9" markerHeight = "9" refX = "7" refY = "3" orient = "auto" >
< path d = "M0,0 L7,3 L0,6 Z" style = "fill:var(--muted)" / >
< / marker >
< / defs >
< text x = "24" y = "34" font-size = "12.5" font-weight = "700" style = "fill:var(--ok)" > 直接调用:目标地址写在指令里< / text >
< rect x = "24" y = "46" width = "230" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "139" y = "75" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > CALL 0x4a1c00( 常量) < / text >
< path d = "M254,70 L298,70" style = "fill:none;stroke:var(--muted);stroke-width:1.6" marker-end = "url(#ar12c)" / >
< rect x = "298" y = "46" width = "210" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "403" y = "75" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > 方法体(可被内联)< / text >
< text x = "528" y = "75" font-size = "12" style = "fill:var(--muted)" > 1 跳, 0 次额外访存< / text >
< text x = "24" y = "140" font-size = "12.5" font-weight = "700" style = "fill:var(--warn)" > 接口调用:目标地址存在内存里,每调一次读一次< / text >
< rect x = "24" y = "152" width = "140" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "94" y = "181" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > load tab< / text >
< path d = "M164,176 L192,176" style = "fill:none;stroke:var(--muted);stroke-width:1.6" marker-end = "url(#ar12c)" / >
< rect x = "192" y = "152" width = "170" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "277" y = "181" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > load fun[i]< / text >
< path d = "M362,176 L390,176" style = "fill:none;stroke:var(--muted);stroke-width:1.6" marker-end = "url(#ar12c)" / >
< rect x = "390" y = "152" width = "150" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "465" y = "181" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > CALL 寄存器< / text >
< path d = "M540,176 L568,176" style = "fill:none;stroke:var(--muted);stroke-width:1.6" marker-end = "url(#ar12c)" / >
< rect x = "568" y = "152" width = "126" height = "48" rx = "8" style = "fill:var(--card);stroke:var(--line)" / >
< text x = "631" y = "181" text-anchor = "middle" font-size = "12.5" style = "fill:var(--ink)" > 方法体< / text >
< text x = "24" y = "232" font-size = "12.5" style = "fill:var(--muted)" > 两处都是内存读:方法表在只读段、数据在堆上,各一次 cache 访问;内联到此为止< / text >
< / svg >
< div class = "figcap" > < b > 图 3< / b > · 接口调用多的不是「查方法名」,而是两次内存读和断掉的内联< / div >
< / figure >
2026-10-21 14:11:30 +08:00
< div class = "tablewrap" >
< table >
2026-10-25 17:16:48 +08:00
< thead > < tr > < th > 开销< / th > < th > 来源< / th > < th > 本机量级 / 条件< / th > < / tr > < / thead >
2026-10-21 14:11:30 +08:00
< tbody >
2026-10-25 17:16:48 +08:00
< tr > < td > 间接跳转< / td > < td > 先读 fun[i] 再 call 寄存器< / td > < td > 0.99 → 5.1 ns/op, 且内联到此为止< / td > < / tr >
< tr > < td > 装箱分配< / td > < td > 值赋给接口时拷一份到堆< / td > < td > 指针与 0– 255 整数 0 B; 字符串、两字结构体 16 B/op< / td > < / tr >
< tr > < td > 访存分离< / td > < td > 方法表在只读段、数据在堆上< / td > < td > 每次调用多两次内存读,热点上放大< / td > < / tr >
2026-10-21 14:11:30 +08:00
< / tbody >
< / table >
< / div >
2026-10-25 17:16:48 +08:00
< p > 比跳转更隐蔽的是< b > 内联没了< / b > : 具体类型的方法能被内联进调用方, 接口调用在编译期不知道目标, 热点里这层间接还会挡住边界检查消除和常量传播。PGO 能救一部分——1.21 起正式 GA, 去虚拟化可以把热点接口调用改回直接调用, 但别把性能寄托在它上面。< / p >
< pre data-lang = "go" data-name = "main_test.go: 同一方法的两条调用路径" > //go:noinline
func callDirect(p *Point) string { return p.Name() }
2026-10-21 14:11:30 +08:00
2026-10-25 17:16:48 +08:00
//go:noinline
func callIface(s fmt.Stringer) string { return s.Name() }< / pre >
< pre data-lang = "text" data-name = "go test -bench . -benchmem( 跑三次取中间值) " > BenchmarkDirect-20 1000000000 0.99 ns/op 0 B/op 0 allocs/op
BenchmarkIface-20 230000000 5.10 ns/op 0 B/op 0 allocs/op< / pre >
< p > < span class = "tag" > 代价< / span > 所以真正的代价不是「慢几纳秒」,而是< b > 更难优化的形状< / b > :多一层间接、多一次分配,优化器能做的事就少一层。反过来,把接口从热路径上挪开,改的往往是结构而不是一行代码——先 profile 再动手,别为 3 ns 重写架构。< / p >
< p class = "take" > < b > 一句话:< / b > 接口调用贵在「从内存取地址再间接跳」和「断掉的内联」;纳秒级开销只在每秒百万次的热点上才值得动刀。< / p >
2026-10-21 14:11:30 +08:00
< h3 id = "sec-12-7" > 12.7 泛型与接口: Go 1.18 之后怎么选< / h3 >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 现象< / span > 同一个求和,一份用类型参数、一份用 < code > []any< / code > 加断言,都不额外分配时 512 个元素是 164 ns 对 289 ns, 差 1.8 倍;如果数据本来就得先装进 < code > []any< / code > ,每格多一个 16 字节的接口头、每个整数再多 8 字节堆对象,< b > 内存先翻三倍< / b > 。< / p >
< p > < span class = "tag" > 机制< / span > 接口是< b > 运行时多态< / b > :具体类型运行时才知道,方法通过方法表调用。泛型是< b > 编译期多态< / b > :类型参数编译期确定,同一份逻辑按类型形状各生成一份,调用能直接展开、能内联。< b > 一个把决定推到运行,一个把决定提前到编译。< / b > < / p >
< pre data-lang = "go" data-name = "泛型约束 vs 接口" > type Number interface {
2026-10-21 14:11:30 +08:00
~int | ~int64 | ~float64
}
func Sum[T Number](xs []T) T {
var s T
for _, x := range xs {
2026-10-25 17:16:48 +08:00
s += x // 编译期就知道 + 可用
2026-10-21 14:11:30 +08:00
}
return s
}
2026-10-25 17:16:48 +08:00
// 对照:运行时才知道类型,每次都要断言
2026-10-21 14:11:30 +08:00
func SumAny(xs []any) any {
var s float64
for _, x := range xs {
2026-10-25 17:16:48 +08:00
s += x.(float64)
2026-10-21 14:11:30 +08:00
}
return s
}< / pre >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 维度< / th > < th > 接口< / th > < th > 泛型< / th > < / tr > < / thead >
< tbody >
2026-10-25 17:16:48 +08:00
< tr > < td > 绑定时机< / td > < td > 运行时< / td > < td > 编译期(按 GC shape 实例化)< / td > < / tr >
2026-10-21 14:11:30 +08:00
< tr > < td > 异构集合< / td > < td > 天然支持< / td > < td > 不支持,一个实例一种类型< / td > < / tr >
< tr > < td > 调用开销< / td > < td > 间接调用 + 可能装箱< / td > < td > 接近直接调用,可内联< / td > < / tr >
< tr > < td > 代码体积< / td > < td > 一份< / td > < td > 按类型形状生成,可能膨胀< / td > < / tr >
< tr > < td > 典型用途< / td > < td > 插件、依赖倒置、错误处理< / td > < td > 容器、算法、类型安全的工具函数< / td > < / tr >
< / tbody >
< / table >
< / div >
2026-10-25 17:16:48 +08:00
< p > 再往下一层,「按类型形状生成」是 1.18 的实现细节:指针形状相同的类型共用一份实例化代码,所以 < code > []int64< / code > 和 < code > []float64< / code > 可能共用同一份, 性能并非按类型完美特化。版本账记住结论就行: 1.18 有泛型, 1.21 起性能与工具链才基本补齐。< / p >
< p > < span class = "tag" > 代价< / span > 泛型不是免费的:每种形状一份代码,二进制会变大;类型参数不能用在方法上(到 1.26 仍不支持泛型方法);约束接口只能描述类型集合,不能当值传递。接口的代价则是每次调用多一跳。< b > 两边都在拿确定性换灵活。< / b > < / p >
< p class = "take" > < b > 一句话:< / b > 需要在运行时替换实现就用接口,只是想让一段逻辑适配几种类型就用泛型——判断标准是「类型在什么时候定下来」。< / p >
2026-10-21 14:11:30 +08:00
< h3 id = "sec-12-8" > 12.8 工程用法与常见错误< / h3 >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 现象< / span > 一个 12 个方法的仓储接口,写第一个 mock 就要补 12 个空方法;按调用方的需要拆成几个小接口后,同一个 mock 只剩两三个方法,生产代码一行没改。< b > 接口的大小直接决定测试的摩擦。< / b > < / p >
< p > < span class = "tag" > 机制< / span > 把依赖收敛成能力: 每个使用方只声明自己用得到的方法, 接口放在使用方所在的包, 实现方反向依赖它。切出来的接口天然小, mock 天然短,依赖图也不会绕圈。< / p >
2026-10-21 14:11:30 +08:00
< pre data-lang = "go" data-name = "按使用方需求切接口" > type OrderRepo interface {
Create(ctx context.Context, o *Order) error
Get(ctx context.Context, id int64) (*Order, error)
}
type OrderNotifier interface {
Notify(ctx context.Context, o *Order) error
}
type OrderService struct {
repo OrderRepo
notifier OrderNotifier
}< / pre >
2026-10-25 17:16:48 +08:00
< p > < span class = "tag" > 代价< / span > 拆得太碎,同一个实现要满足一堆小接口,装配代码会变长;接口满天飞时,读代码要来回跳。经验值是每个接口一到三个方法,超过就想想是不是该按调用方拆。< / p >
2026-10-21 14:11:30 +08:00
< div class = "box warn" >
2026-10-25 17:16:48 +08:00
< span class = "bt" > 易错点 · 用 == 比较接口,可能直接 panic< / span >
< p > 接口值能比较,比的是「类型相同且值相等」。动态类型本身不可比较时(< code > []int< / code > 、map、函数装进 < code > any< / code > ) , < code > ==< / code > 会在运行时崩:< code > comparing uncomparable type []int< / code > 。它和判 nil 还是两件事——装了 nil 指针的接口,既不是 nil, 也不等于另一个空接口。< / p >
< / div >
< div class = "box note" >
2026-10-21 14:11:30 +08:00
< span class = "bt" > 五个高频错误< / span >
2026-10-25 17:16:48 +08:00
< p > ① 返回具体类型却声明成接口,调用方被迫断言;② 接口定义在实现方包里,引来循环依赖;③ 接口方法过多, mock 全变空方法;④ 用空接口当万能参数,丢掉类型检查;⑤ 忽略带类型的 nil, 写出「err 非 nil 但内容为空」。< / p >
< / div >
< p class = "take" > < b > 一句话:< / b > 接口定义在使用方、越小越好;返回接口时只返回字面 nil 或真正有值的对象;要比较内容就先断言到具体类型,别拿 < code > ==< / code > 硬撞。< / p >
< h2 id = "ch12-sum" > 全章回顾:接口的四笔账< / h2 >
< div class = "box case" >
< span class = "bt" > 三句话收口< / span >
< p > < b > ①< / b > 接口值永远是两块字:类型信息(非空接口是方法表)加数据指针,一共 16 字节。< / p >
< p > < b > ②< / b > 赋值等于装箱加查方法表,查一次就永久复用;装箱是拷贝,装进去的值和外面那个再无关系。< / p >
< p > < b > ③< / b > 所有坑都能追到同一件事:接口只认「类型 + 值」,而 nil 只认「两块都空」。< / p >
< / div >
< div class = "tablewrap" >
< table >
< thead > < tr > < th > 症状< / th > < th > 先查什么< / th > < th > 回哪节< / th > < / tr > < / thead >
< tbody >
< tr > < td > 明明没出错, err != nil 却成立< / td > < td > 有没有 return 一个值为 nil 的具体指针< / td > < td > 12.5< / td > < / tr >
< tr > < td > 接口里取出来的值和预期不一样< / td > < td > 装箱时拷的是值还是指针,之后改过谁< / td > < td > 12.3< / td > < / tr >
< tr > < td > 断言一直 false, 类型看着明明对< / td > < td > 是不是被 %w 或别的包装器套了一层< / td > < td > 12.4< / td > < / tr >
< tr > < td > 值类型传进接口后状态没变< / td > < td > 方法是不是写成了值接收者< / td > < td > 12.3< / td > < / tr >
< tr > < td > 热点函数 profile 出接口调用开销< / td > < td > 能不能改成泛型或具体类型,别急着改架构< / td > < td > 12.6< / td > < / tr >
< tr > < td > 比较两个接口时 panic< / td > < td > 动态类型是不是 slice、map 或函数< / td > < td > 12.8< / td > < / tr >
< / tbody >
< / table >
2026-10-21 14:11:30 +08:00
< / div >
< div class = "box tip" >
2026-10-25 17:16:48 +08:00
< span class = "bt" > 面试怎么收尾< / span >
< p > 先给两句骨架:< b > 接口是隐式契约,运行时是两个机器字< / b > ; 再按问题往深里走——赋值讲装箱和方法表, nil 讲两块字都为空,性能讲间接跳转和内联。最后主动补一句「所以接口定义在使用方、越小越好,热路径上该用泛型」,把话题引到你有实战的地方。< / p >
2026-10-21 14:11:30 +08:00
< / div >
< / article >
< nav class = "pager" >
< a class = "prev" href = "11-context.html" > < span class = "k" > ← 上一章< / span > < span class = "v" > 第 11 章 · context: 取消、超时与 goroutine 泄露防治< / span > < / a >
< a class = "next" href = "13-puzzles.html" > < span class = "k" > 下一章 →< / span > < span class = "v" > 第 13 章 · 并发编程题:从交替打印到 worker pool< / span > < / a >
< / nav >
< p class = "foot" >
Go 八股 · 第 12 章 接口:隐式契约与运行时表示< br >
< a href = "index.html" > 总目录< / a > · < a href = "../../SRE工程师/sre-learning-path/index.html" > SRE 学习路径< / a >
< / p >
< / 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 >