🐹 Go Quick Reference

📐 工作原理与说明
按分类聚合条目:遍历条目列表,首次出现的分类记入有序数组并把计数置 1,后续同分类累加;柱状图以分类名为横轴、条目数为数据(图高 300),明细表逐行输出序号、分类名、该分类条目数与该分类前 2 条代表命令(以「 / 」连接)。分类名在英文态下按内置映射表替换为英文(Go 语法与标准库)
Go 语言常用语法与标准库速查。
  • 核心:变量与常量、结构体、方法、接口、defer、panic/recover。
  • 并发:go 关键字启动 goroutine,channel 通信,select 多路复用。
  • 错误:显式返回 error,用 errors.Is/errors.As 判定;Go 没有异常机制。
  • 常用包:fmt、strings、strconv、time、encoding/json、net/http、sync。

Browse a Go cheat sheet in your browser — syntax, concurrency and standard-library snippets.

📚 深度解析:Go 速查

💡 常见使用场景

  • 查阅 goroutine/defer/err 处理。
  • 写并发代码时查 channel 与 select 的惯用法,确认关闭 channel 与 range 的配合,避免写出会永久阻塞的 goroutine。
查错误
`if err != nil { return err }` 惯用法。

❓ 常见问题(FAQ)

defer 何时执行?
函数返回前按后进先出执行。
goroutine 泄漏怎么避免?
核心是保证每个 goroutine 都有退出路径:用 context 传递取消信号、给 channel 操作加 select + ctx.Done() 分支、并确保发送方最终会关闭 channel。常见于等待一个永远没人写入的 channel——可用 pprof 观察 goroutine 数量是否持续增长。

Formula & Standard References

Item Formula / Note Source / Standard
Specification syntax and semantics defined by the Go Language Specification Go Language Specification
Concurrency goroutines with channels for communication Go Language Specification
Error handling errors are interface values returned and checked explicitly Go Language Specification
⚠️ 使用说明与注意事项