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的更多相关文章

  1. 【GoLang】golang 闭包 closure 参数传递的蹊跷!

    结论: 闭包函数可以直接引用外层代码定义的变量, 但是,注意,闭包函数里面引用的是变量的地址, 当goroutine被调度时,改地址的值才会被传递给goroutine 函数. 介绍 go的闭包是一个很 ...

  2. Go并发控制之sync.WaitGroup

    WaitGroup 会将main goroutine阻塞直到所有的goroutine运行结束,从而达到并发控制的目的.使用方法非常简单,真心佩服创造Golang的大师们! type WaitGroup ...

  3. go语言之并发

    简介           多核处理器越来越普及,那有没有一种简单的办法,能够让我们写的软件释放多核的威力?答案是:Yes.随着Golang, Erlang, Scale等为并发设计的程序语言的兴起,新 ...

  4. [转载] Go语言并发之美

    原文: http://qing.blog.sina.com.cn/2294942122/88ca09aa33002ele.html 简介           多核处理器越来越普及,那有没有一种简单的办 ...

  5. [转Go-简洁的并发 ]

    http://www.yankay.com/go-clear-concurreny/ Posted on 2012-11-28by yankay 多核处理器越来越普及.有没有一种简单的办法,能够让我们 ...

  6. go语言channel的别样用法

    1.返回值使用通道 func main() { // 生成随机数作为一个服务 randService := randGenerator() // 从服务中读取随机数并打印 fmt.Printf(&qu ...

  7. golang闭包里的坑

    介绍 go的闭包是一个很有用的东西.但是如果你不了解闭包是如何工作的,那么他也会给你带来一堆的bug.这里我会拿出Go In Action这本书的一部分代码,来说一说在使用闭包的时候可能遇到的坑.全部 ...

  8. golang 的 sync.WaitGroup

    WaitGroup的用途:它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成. 官方对它的说明如下: A WaitGroup waits for ...

  9. Go语言并发之美

    简介           多核处理器越来越普及,那有没有一种简单的办法,能够让我们写的软件释放多核的威力?答案是:Yes.随着Golang, Erlang, Scale等为并发设计的程序语言的兴起,新 ...

随机推荐

  1. C#未能找到路径“\bin\roslyn\csc.exe”的一部分。

    主要原因是因为两个库存在,需要生成一个 roslyn文件,但是这个项目是从 vs2017中,打开的,所以,没有必要存在它. 那么就删除这两个关联的库,就可以达到目的 S2017 打开  程序包管理控制 ...

  2. 安装简易OpenShift单节点并配置DNS泛域名

    1 楔子 公司有个项目部署在 OpenShift 容器云平台上,出现问题较多,于是萌生在公司环境搭建现场仿真环境,由于资源没有生产环境多,就使用单节点简单模拟下 本文主要内容包括以下: 使用 Dnsm ...

  3. Oracle创建表空间创建用户授权

    注意:SYS用户下执行.sys登录必须为sysdba身份.查看数据文件存放位置. select * from dba_data_files; 1.创建表空间 CREATE TABLESPACE tp_ ...

  4. VC维相关知识

    假设空间H(Hypothesis Set) 输入空间D(X1...Xn) 1.增长函数(grown function) 是关于输入空间尺寸n的函数 假设空间对于D中所有实例实现分类(赋予标记)的分类方 ...

  5. [LeetCode]690. Employee Importance员工重要信息

    哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...

  6. CyclicBarrier(栅栏)的用法详解及与countDownLatch用法区别

    CyclicBarrier适用于这样的情况:你希望创建一组任务,它们并行的执行工作,然后在进行下一步步骤之前等待,直至所有任务都完成,它使得所有的并行任务都将在删栏出列队,因此可以一致的向前移动. 当 ...

  7. 企业集群架构-03-NFS

    NFS 目录 NFS NFS基本概述 NFS应用场景 NFS实现原理 NFS总结 NFS服务端安装 环境准备 服务端安装NFS 服务端NFS配置 服务端开机自启 服务端验证配置 NFS客户端挂载卸载 ...

  8. 别再费劲去找后台的前端框架了,2021 年就用 Fantastic-admin 吧

    前言 你知道光是基于 Vue 的后台框架在 Github 上有多少个仓库么? 如果你搜索 vue admin 会得到 13120 个仓库,如果用 vue 后台 会得到 7596 个仓库,如果把两者结合 ...

  9. 技术基础 | 监测Apache Cassandra的简明方式——MCAC

    点击这里在GitHub上访问我们,以便深入了解DataStax的开源项目--Apache Cassandra指标收集器(Metric Collector for Apache Cassandra, o ...

  10. windows中关闭端口的方法

    打开cmd:输入-ano | findstr "端口号" 控制台会输出占用端口的pid 如"8380" 再向cmd中输入 taskkill /f/pid 838 ...