golangci-lint
什么是lint?
answer: 静态代码分析。通俗地讲,扫描源代码,在不运行代码的情况下,找出一些不规范的书写方式以修正.
fmt.Sprintf("%d", "123") // 错误
fmt.Sprintf("%s", "123") // 正确
for i, _ := range []int{1,2,3}{} // 错误
for i := range []int{1,2,3}{} // 正确
a := make([]int, 0, 0) // 错误
a := make([]int, 0) // 正确
for _, v := range vs { // 错误
vectors = append(vectors, v)
}
vectors = append(vectors, vs...) // 正确
怎样使用lint
- 执行扫描工作的程序是一个bin文件(称为linter)。执行它就可以扫描项目源代码给出提示。在Go生态内的linter有很多种,大部分是开源项目提供的,它们能扫描各式各样的问题如“废弃代码”,“冗余的写法”,“调用deprecated api”等。
- 经调研,在Go开发中最好的选择不是使用某一种linter,而是使用Golangci-lint,他是一个开源的linter框架,作用类似手机上的应用市场,你可以通过配置选择开启五花八门的linter,兼顾了使用灵活、功能强大。
- golangci-lint的安装使用见后文。
怎样将lint集成到团队工作流?
- 团队工作中代码仓库是共享的,一个人跑lint没用,需要大家遵循同一套lint规则。
- 具体办法是,将linter的运行加入到ci流程中。任意开发push新代码,都会触发lint,结果展示在merge request界面。如果存在bad case,就不允许merge到master分支。这样协作项目的代码就能始终符合lint规范。
实践介绍
背景: 团队代码库,因为历史遗留小问题很多,肉眼排查不实际,想到引入lint解决该问题。
**ci工具选择:**linter如上介绍使用Golangci-lint。公司使用Gitlab作为源码管理平台,ci工具使用它提供的 Gitlabci。
下面是我自己做的准备工作
1.安装Golangci-lint,命令如下,详细说明可参考其README
one way
GOPATH_FIRST=$(echo $(go env GOPATH) | awk -F':' '{ print $1}')
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ${GOPATH_FIRST}/bin v1.21.0
two way
go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.41.1
other ways 更多安装方式
2.配置Golangci-lint
在项目根目录下创建文件.golangci.yml,这个文件是golangci-lint的配置文件,可自定义 开启/关闭哪些linter、跳过某些文件、跳过某些lint规则等。
如无配置文件,Golangci-lint会开启一些预设linter,这些在其文档内有说明。
下面是我们组一个项目现在的.golangci.yml内容,
linters:
disable-all: true # 关闭其他linter
enable: # 下面是开启的linter列表,之后的英文注释介绍了相应linter的功能
- deadcode # Finds unused code
# - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- gosimple # Linter for Go source code that specializes in simplifying a code
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- ineffassign # Detects when assignments to existing variables are not used
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
- structcheck # Finds unused struct fields
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
- unused # Checks Go code for unused constants, variables, functions and types
- varcheck # Finds unused global variables and constants
- scopelint # Scopelint checks for unpinned variables in go programs
# - golint # Carry out the stylistic conventions put forth in Effective Go and CodeReviewComments
linters-settings:
govet: # 对于linter govet,我们手动开启了它的某些扫描规则
check-shadowing: true
check-unreachable: true
check-rangeloops: true
check-copylocks: true
3.运行linter/修正错误
在项目根目录下运行golangci-lint run,
该命令会加载.golangci.yml(如有),执行lint。如代码有问题,命令行会输出错误提示,包括犯错地点、修正方法,有时linter提示会比较含糊,拿关键字google一下能找到详细说明
4.推广
为做到平滑的推广lint,在操作时有下面的实践
分批放开linter,先启用规则简单的linter例如deadcode,unused,之后开启规则更严格的linter例如gosimple。以使同事逐渐适应。
在开启一个linter前,我会手动修复该linter对应的的所有错误。确保之后发起mr的同事只需为新改动负责。
5. **与gitlabci集成,**走到这里lint工具的准备就ok了,下面要将它和ci工具衔接在一起,
参考Gitlabci文档,安装gitlabci runner
将它和gitlab project绑定在一起。自行安装gitlab runner有点复杂,我们公司的工程效率团队提供了公用的docker runner,我这里只要做好绑定就行。
在项目根目录下创建一个名为.gitlab-ci.yml的配置文件,这样每当push代码时,gitlab检测到有该文件,就会触发runner执行ci。我的.gitlab-ci.yml文件内容如下
image: registry.company.cn/ee/go:1.12.9-4
stages:
- qa
lint:
stage: qa
script:
- golangci-lint runGitLab CI/CD | GitLabimage: registry.company.cn/ee/go:1.12.9-4
stages:
- qa
lint:
stage: qa
script:
- golangci-lint run