Go之禅 - 基于Rob Pike思想的Go语言哲学
1. 简单胜过聪明 (Simple is better than clever)
不要炫技,要解决问题
|
|
2. 清晰胜过简洁 (Clear is better than concise)
代码首先要让人读懂
|
|
3. 组合胜过继承 (Composition over inheritance)
Go的接口和嵌入体现组合思想
|
|
4. 接口要小而专注 (Interfaces should be small and focused)
接口越小越好,单一职责
|
|
5. 并发不是并行 (Concurrency is not parallelism)
并发是关于结构,并行是关于执行
|
|
6. 通过通信来共享内存,而不是通过共享内存来通信 (Share memory by communicating)
Channel是Go的核心哲学
|
|
7. 错误是值,不是异常 (Errors are values, not exceptions)
显式错误处理胜过异常
|
|
8. 不要设计大型接口 (Don't design with interfaces, discover them)
接口应该从使用中发现,而不是预先设计
|
|
9. 空接口什么都不说 (The empty interface says nothing)
避免过度使用interface{}
|
|
10. Gofmt的风格就是每个人的风格 (Gofmt's style is no one's favorite, yet gofmt is everyone's favorite)
统一的代码格式胜过个人偏好
|
|
11. 小规模清晰胜过大规模复杂 (A little copying is better than a little dependency)
适度的代码重复胜过不必要的依赖
|
|
12. 系统调用、操作系统线程和互斥锁的代价很高,尽量避免 (Syscalls, OS threads, and mutexes are expensive)
理解并发原语的成本
|
|
13. 缓存是有用的 (Caching is important)
但要简单明了
|
|
14. 测试先行,但不要过度测试 (Test first, but don't over-test)
测试重要行为,不是实现细节
|
|
15. 如果你觉得需要泛型,请三思 (If you think you need generics, think again)
Go 1.18+有了泛型,但要谨慎使用
|
|
16. 性能问题通常出在算法,不是语言 (Performance problems are usually algorithmic)
优化算法比优化语言特性更重要
|
|
17. 数据结构,不是算法,才是编程的核心 (Data structures, not algorithms, are central to programming)
正确的数据结构让程序简单明了
|
|
Go之禅总结: Go语言强调简单、清晰、组合和并发。Rob Pike的设计哲学体现在语言的每个角落:小而专注的接口、显式的错误处理、通过通信共享内存的并发模型,以及对简单性的坚持。Go不追求语言特性的丰富性,而是追求解决实际问题的有效性。
