Before you launch a goroutine, know when it will stop The Zen of Go
The Zen of Go https://the-zen-of-go.netlify.app/
Ten engineering values for writing simple, readable, maintainable Go code. Presented at GopherCon Israel 2020.
- Each package fulfils a single purpose
-
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
-
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
-
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
-
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.
- 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?
Before you launch a goroutine, know when it will stop The Zen of Go的更多相关文章
- 【GoLang】golang 闭包 closure 参数传递的蹊跷!
结论: 闭包函数可以直接引用外层代码定义的变量, 但是,注意,闭包函数里面引用的是变量的地址, 当goroutine被调度时,改地址的值才会被传递给goroutine 函数. 介绍 go的闭包是一个很 ...
- Go并发控制之sync.WaitGroup
WaitGroup 会将main goroutine阻塞直到所有的goroutine运行结束,从而达到并发控制的目的.使用方法非常简单,真心佩服创造Golang的大师们! type WaitGroup ...
- go语言之并发
简介 多核处理器越来越普及,那有没有一种简单的办法,能够让我们写的软件释放多核的威力?答案是:Yes.随着Golang, Erlang, Scale等为并发设计的程序语言的兴起,新 ...
- [转载] Go语言并发之美
原文: http://qing.blog.sina.com.cn/2294942122/88ca09aa33002ele.html 简介 多核处理器越来越普及,那有没有一种简单的办 ...
- [转Go-简洁的并发 ]
http://www.yankay.com/go-clear-concurreny/ Posted on 2012-11-28by yankay 多核处理器越来越普及.有没有一种简单的办法,能够让我们 ...
- go语言channel的别样用法
1.返回值使用通道 func main() { // 生成随机数作为一个服务 randService := randGenerator() // 从服务中读取随机数并打印 fmt.Printf(&qu ...
- golang闭包里的坑
介绍 go的闭包是一个很有用的东西.但是如果你不了解闭包是如何工作的,那么他也会给你带来一堆的bug.这里我会拿出Go In Action这本书的一部分代码,来说一说在使用闭包的时候可能遇到的坑.全部 ...
- golang 的 sync.WaitGroup
WaitGroup的用途:它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成. 官方对它的说明如下: A WaitGroup waits for ...
- Go语言并发之美
简介 多核处理器越来越普及,那有没有一种简单的办法,能够让我们写的软件释放多核的威力?答案是:Yes.随着Golang, Erlang, Scale等为并发设计的程序语言的兴起,新 ...
随机推荐
- 爱普生 L4160 Serveies 网络打印机配置(问题解决)
一.爱普生网络打印机固定IP地址 用网络打印机过程中,偶尔会出现打印机脱机的状况,大多数原因是打印机的IP地址在路由器重启过后重新分配了IP地址导致的.此时,为了减少不必要的麻烦就需要固定打印机的IP ...
- 痞子衡嵌入式:MCUXpresso IDE下SDK工程导入与workspace管理机制
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是MCUXpresso IDE下SDK工程导入与workspace管理机制. MCUXpresso IDE是恩智浦软件团队倾注很大心血研发 ...
- 通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明 解决办法
直接升级dubbo的版本到2.6.4 下面的是我的项目的pom.xml配置的依赖 <dependency> <groupId>com.alibab ...
- Oracle中除数为0的两种解决办法(decode与nullif)
Oracle中Decode函数,语句DECODE(tag,''ZCGS'',0,1)=decode(''@corp-No@'',''6010'',1,0) decode(字段或字段的运算,值1,值2, ...
- Flash Player的终章——赠予它的挽歌
本文由葡萄城技术团队原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 12月28日消息,微软已经确认Windows 10在下一次更新时将自动删除F ...
- PP主数据-物料主数据
一.PP物料主数据:PP的物料主数据,是对应到系统的组织架构的,不同的组织层次,都有各自的主数据需要创建. (1),一般数据:一般数据是在集团层面的主数据,主要包括:物料编码.物料描述.辅助计量单位以 ...
- JavaDailyReports10_06
今日收获: 一.所有引用类型变量的初始化一定要使用new 关键字定义声明,空指针异常的错误原因可能是变量没有初始化导致的. 每一个类体的数据成员一定要在实例化的同时赋值,用一个实例化的类实现问题中最小 ...
- 动态REM
什么是rem? rem是相对于根元素html字体大小来计算的,即( 1rem = html字体大小 ) rem和em区别? rem:(root em,根em)根元素的fort-size的大小计算em: ...
- Kubernetes K8S之通过helm部署metrics-server与HPA详解
Kubernetes K8S之通过helm部署metrics-server与 Horizontal Pod Autoscaling (HPA)详解 主机配置规划 服务器名称(hostname) 系统版 ...
- IDEA maven项目报错,找不到或无法找到主类
最近在写UDF,依赖复制的公司的依赖, <dependency> <groupId>org.apache.hive</groupId> <artifactId ...