词表 · 第 2 篇
Go
1. buffered channel
- 译:缓冲通道 | Go:带缓冲队列的 channel,缓冲未满时发送不阻塞
- 例
- A buffered channel blocks only when full. — 缓冲通道只在满了时才阻塞发送。
- Send to a buffered channel without waiting. — 向缓冲通道发送无需等待。
- 地道:buffered vs unbuffered(缓冲与非缓冲)
- 短语:channel buffer · make(chan, n) · unbuffered channel
2. channel
- 译:通道 | Go:goroutine 之间传递数据的并发安全管道
- 例
- Channels let goroutines communicate safely. — channel 让 goroutine 安全通信。
- Send a value into the channel. — 把一个值发送进 channel。
- 地道:share memory by communicating(靠通信共享内存)
- 短语:send / receive on a channel · close a channel · channel direction
3. context
- 译:上下文 | Go:携带超时、取消信号和请求范围值的对象
- 例
- Pass context to every blocking call. — 把 context 传给每个阻塞调用。
- The context cancels on timeout. — 超时时 context 会取消。
- 地道:context with cancel(带取消的 context)
- 短语:context.Background · context.WithTimeout · context propagation
4. defer
- 译:延迟;defer 语句 | Go:在函数返回时才执行的延迟调用
- 例
- Use defer to close files. — 用 defer 关闭文件。
- Deferred calls run in LIFO order. — defer 的调用按后进先出执行。
- 地道:defer cleanup(延迟清理)
- 短语:defer statement · defer close · defer in a loop
5. embedding
- 译:嵌入 | Go:通过结构体匿名字段实现”组合式继承”
- 例
- Struct embedding promotes fields and methods. — 结构体嵌入会提升字段与方法。
- Embedding is Go’s take on inheritance. — 嵌入是 Go 对继承的诠释。
- 地道:embed, don’t inherit(用嵌入而非继承)
- 短语:struct embedding · interface embedding · embed a type
6. escape analysis
- 译:逃逸分析 | Go:编译器决定变量分配在栈还是堆的分析
- 例
- Escape analysis decides heap vs stack. — 逃逸分析决定分配在堆还是栈。
- Returning a pointer makes the value escape. — 返回指针会让该值逃逸。
- 地道:escapes to the heap(逃逸到堆上)
- 短语:escape to heap · stack allocation · compiler analysis
7. go mod
- 译:go mod | Go:模块与依赖管理命令
- 例
- Run
go mod tidyto sync deps. — 运行go mod tidy同步依赖。 go mod initcreates go.mod. —go mod init创建 go.mod。
- Run
- 地道:tidy your modules(整理你的模块)
- 短语:go mod tidy · go mod init · go.mod / go.sum
8. goroutine
- 译:goroutine | Go:由运行时管理的轻量级线程
- 例
- Spin up a goroutine with
go f(). — 用go f()启动一个 goroutine。 - A goroutine costs only a few KB. — 一个 goroutine 仅占几 KB。
- Spin up a goroutine with
- 地道:goroutine leak(goroutine 泄漏)
- 短语:spawn a goroutine · goroutine leak · goroutine scheduler
9. interface
- 译:接口 | Go:一组方法签名构成的隐式契约
- 例
- Go interfaces are satisfied implicitly. — Go 的接口是隐式实现的。
- The empty interface holds any value. — 空 interface 可承载任意值。
- 地道:interface satisfied implicitly(接口被隐式满足)
- 短语:empty interface · interface satisfaction · io.Reader
10. map
- 译:映射;map 类型(Go:键值对集合 / 哈希表)
- 短语:map[K]V · map literal · range over a map
11. mutex
- 译:互斥锁 | Go:保证同一时刻仅一个 goroutine 进入临界区的锁
- 例
- Guard shared state with a mutex. — 用 mutex 保护共享状态。
- Lock the mutex before writing. — 写之前锁住 mutex。
- 地道:lock and unlock(加锁与解锁)
- 短语:sync.Mutex · RWMutex · lock / unlock
12. package
- 译:包(Go:组织代码、控制可见性的命名集合)
- 短语:package main · import a package · exported identifier
13. pointer
- 译:指针 | Go:保存变量地址、可间接访问其值的类型
- 例
- Pass a pointer to avoid copying. — 传指针以避免拷贝。
- A nil pointer has no target. — nil 指针没有指向目标。
- 地道:pass by pointer(按指针传递)
- 短语:pointer to · nil pointer · pointer receiver
14. receiver
- 译:接收者 | Go:方法所绑定、调用时传入的实例
- 例
- Choose pointer or value receiver carefully. — 谨慎选择指针或值接收者。
- The receiver appears before the method name. — 接收者出现在方法名之前。
- 地道:value vs pointer receiver(值接收者与指针接收者)
- 短语:pointer receiver · value receiver · receiver type
15. select
- 译:select 语句 | Go:同时等待多个 channel 操作的多路复用
- 例
- select waits on multiple channels. — select 同时等待多个 channel。
- The default case makes select non-blocking. — default 分支让 select 非阻塞。
- 地道:select on channels(在 channel 上 select)
- 短语:select statement · default case · multiplex channels
16. slice
- 译:切片 | Go:引用底层数组、可动态伸缩的序列
- 例
- A slice grows when you append. — append 时切片会扩容。
- Slices share the same backing array. — 切片共享同一底层数组。
- 地道:slice header(切片头)
- 短语:make a slice · append to a slice · slice bounds
17. struct
- 译:结构体 | Go:把命名字段聚合在一起的复合类型
- 例
- A struct groups related fields. — 结构体把相关字段聚合在一起。
- Initialize a struct with field names. — 用字段名初始化结构体。
- 地道:plain old struct(朴素的 struct)
- 短语:struct literal · anonymous struct · embed a struct
18. sync
- 译:同步 | Go:标准库里提供并发原语的包
- 例
- Use sync.WaitGroup to wait for goroutines. — 用 sync.WaitGroup 等待 goroutine。
- sync.Once runs initialization exactly once. — sync.Once 保证初始化只执行一次。
- 地道:sync primitives(同步原语)
- 短语:sync.Mutex · sync.WaitGroup · sync.Once
19. testing
- 译:测试 | Go:标准测试框架与 *_test.go 约定
- 例
- Run tests with
go test. — 用go test运行测试。 - Table-driven tests are idiomatic in Go. — 表驱动测试是 Go 的惯用法。
- Run tests with
- 地道:table-driven tests(表驱动测试)
- 短语:go test · table-driven test · benchmark test
20. type assertion
- 译:类型断言 | Go:从接口值取出其底层具体类型
- 例
- A type assertion extracts the concrete type. — 类型断言取出具体类型。
- Use the comma-ok form to stay safe. — 用 comma-ok 形式更安全。
- 地道:comma-ok idiom(comma-ok 惯用法)
- 短语:type assertion · comma-ok · type switch
21. zero value
- 译:零值 | Go:变量未显式初始化时的默认值
- 例
- The zero value of an int is 0. — int 的零值是 0。
- Make the zero value useful. — 让零值本身可用。
- 地道:make the zero value useful(让零值有意义)
- 短语:zero value · default value · zero-value-safe