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等为并发设计的程序语言的兴起,新 ...
随机推荐
- 华为---MGRE实验--内网为RIP
数据链路层封装技术总结: 实验要求与拓扑: 1. 配置IP地址 公网IP地址:12.1.1.0/24.23.1.1.0/24.24.1.1.0/24 tunnel地址:1.1.1.1/24.1.1.1 ...
- JS拼接字符串太长希望换行保持html格式拼接的方法
1. 通常情况 tabPeoStr +='<tr class="tabPeo"><td>'+data[i].name+'</td><td& ...
- 3.自定义view-TextView变色
1.效果 2.实现原理 自定义Textview,重写onDraw方法,将画布分成两部分,用不同颜色的画笔画 核心代码: @Override protected void onDraw(Canvas c ...
- 关于SM4 加密算法
国密SM4算法 与DES和AES算法相似,国密SM4算法是一种分组加密算法.SM4分组密码算法是一种迭代分组密码算法,由加解密算法和密钥扩展算法组成. SM4是一种Feistel结构的分组密码算法,其 ...
- 【SpringBoot—注解】@requestBody 与@requestparam;@requestBody的加与不加的区别
一)首先说明xia @requestBody与@requestParam的区别 spring的RequestParam注解接收的参数是来自于requestHeader中,即请求头.都是用来获取请求路径 ...
- Keras使用多个GPU并行
model = Model(inputs=[v_i, v_j], outputs=output_list) model = multi_gpu_model(model,4) model.compile ...
- Java学习日报7.24
package tem; public class Tem { public static void main(String[] args) { // TODO 自动生成的方法存根 //每隔10摄氏度 ...
- 学习DOS,个人笔记
在win中\表示根目录, 在linux中/表示根目录 注意: 有些家庭版的系统会选择性的调用命令的,有的命令虽然有那个文件,但是不能使用..... dir 命令 英语全称 ...
- Turtlebot3入门教程-系统-SBC软件设置(ubuntu20.04)
本文针对如何在树莓派3上安装ubuntu20.04系统和软件进行讲解 树莓派3接上显示屏和鼠标后,开机后继续安装软件包 详细步骤如下: (1)系统安装 (2)ROS安装 (3)TurboBot3依赖的 ...
- ABP框架中短信发送处理,包括阿里云短信和普通短信商的短信发送集成
在一般的系统中,往往也有短信模块的需求,如动态密码的登录,系统密码的找回,以及为了获取用户手机号码的短信确认等等,在ABP框架中,本身提供了对邮件.短信的基础支持,那么只需要根据自己的情况实现对应的接 ...