Golang语言之禅
理解
Each package fulfils a single purpose 保持每个package的单一目的性
A well designed Go package provides a single idea, a set of related behaviours. A good Go package starts by choosing a good name. Think of your package’s name as an elevator pitch to describe what it provides, using just one word.
Handle errors explicitly 显式处理errors
Robust programs are composed from pieces that handle the failure cases before they pat themselves on the back.
The verbosity of if err != nil { return err }
is outweighed by the value of deliberately handling each failure condition at the point at which they occur.
Panic and recover are not exceptions, they aren’t intended to be used that way.
Return early rather than nesting deeply 提前返回胜过深嵌套
Every time you indent you add another precondition to the programmer’s stack consuming one of the 7 ±2 slots in their short term memory. Avoid control flow that requires deep indentation. Rather than nesting deeply, keep the success path to the left using guard clauses.
Leave concurrency to the caller 把并发交给调用者
Let the caller choose if they want to run your library or function asynchronously, don’t force it on them. If your library uses concurrency it should do so transparently.
Before you launch a goroutine, know when it will stop 使用goroutine之前想清楚它什么时候结束
Goroutines own resources; locks, variables, memory, etc. The sure fire way to free those resources is to stop the owning goroutine.
Avoid package level state
Seek to be explicit, reduce coupling, and spooky action at a distance by providing the dependencies a type needs as fields on that type rather than using package variables.
Simplicity matters
Simplicity is not a synonym for unsophisticated. Simple doesn’t mean crude, it means readable and maintainable. When it is possible to choose, defer to the simpler solution.
Write tests to lock in the behaviour of your package’s API 编写单元测试,保证Package API行为
Test first or test later, if you shoot for 100% test coverage or are happy with less, regardless your package’s API is your contract with its users. Tests are the guarantees that those contracts are written in. Make sure you test for the behaviour that users can observe and rely on.
If you think it’s slow, first prove it with a benchmark
So many crimes against maintainability are committed in the name of performance. Optimisation tears down abstractions, exposes internals, and couples tightly. If you’re choosing to shoulder that cost, ensure it is done for good reason.
Moderation is a virtue
Use goroutines, channels, locks, interfaces, embedding, in moderation.
thress attribute of Channel
- Gurarantee Of Delivery
- Ticker的实现为什么使用Delayed Guarantee类型的Channel
- Ticker为什么会丢失
- State
- With or Without Data
Maintainability counts 可维护性
Clarity, readability, simplicity, are all aspects of maintainability. Can the thing you worked hard to build be maintained after you’re gone? What can you do today to make it easier for those that come after you?
翻译原文: https://github.com/davecheney/the-zen-of-go
参考
-
The Behavior Of Channels #TODO https://www.ardanlabs.com/blog/2017/10/the-behavior-of-channels.html
-
Go proverbs https://www.kancloud.cn/cserli/golang/524388