go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
如何使用 Gocyclo linter ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Calculate cyclomatic complexities of Go functions.
Usage:
gocyclo [flags] <Go file or directory> ...
Flags:
-over N show functions with complexity > N only and
returnexit code 1if the set is non-empty
-top N show the top N most complex functions only
-avg, -avg-short show the average complexity over all functions; the short option prints the value without a label
-ignore REGEX exclude files matching the given regular expression
The output fields for each line are:
<complexity> <package> <function> <file:line:column>
$ bodyclose
bodyclose is a tool for static analysis of Go programs.
Usage of bodyclose:
bodyclose unit.cfg # execute analysis specified by config file bodyclose help# general help, including listing analyzers and flags bodyclose help name # help on specific analyzer and its flags
$ golangci-lint run --disable-all -E bodyclose main.go
main.go:11:26: response body must be closed (bodyclose) resp, err := http.Get("http://example.com/")
在 Go 的 database/sql 包中,sql.Rows 是一个 struct,用于表示从数据库查询中返回的多行结果。
它提供了一组方法,允许开发者逐行读取查询结果。
迭代结果:使用 Next() 方法逐行遍历结果集。
扫描数据:使用 Scan() 方法将当前行的列值复制到指定的变量中。
关闭结果集:使用 Close() 方法释放与结果集相关的资源。
sqlrows 的官方介绍:
sqlrows is a static code analyzer which helps uncover bugs by reporting a diagnostic for mistakes of sql.Rows usage.
安装
1
go install github.com/gostaticanalysis/sqlrows@latest
如何使用 sqlrows ?
1
2
3
4
5
6
7
$ sqlrows
sqlrows is a tool for static analysis of Go programs.
Usage of sqlrows:
sqlrows unit.cfg # execute analysis specified by config file sqlrows help# general help sqlrows help name # help on specific analyzer and its flags
// main.go
packagekydenimport("context""database/sql")funcf(ctxcontext.Context,db*sql.DB)(interface{},error){rows,err:=db.QueryContext(ctx,"SELECT * FROM users")deferrows.Close()// NG: using rows before checking for errors
iferr!=nil{returnnil,err}// defer rows.Close() // NG: this return will not release a connection.
forrows.Next(){err=rows.Scan()iferr!=nil{returnnil,err}}returnnil,nil}
针对两种 NG 的不同输出:
1
2
3
go vet -vettool=$(which sqlrows) main.go
# command-line-arguments./main.go:10:11: using rows before checking for errors
1
2
3
go vet -vettool=$(which sqlrows) main.go
# command-line-arguments./main.go:9:33: rows.Close must be called
// main.go
packagekydenfuncf()int{a:="This is a very long line that exceeds the maximum line length set by the linter and should be broken up into smaller, more manageable lines."returnlen(a)}
1
2
3
golangci-lint run
main.go:5: the line is 151 characters long, which exceeds the maximum of 80 characters. (lll) a :="This is a very long line that exceeds the maximum line length set by the linter and should be broken up into smaller, more manageable lines."
golangci-lint run
main.go:9:10: Error return value is not checked (errcheck) hello("Kyden") // err Not Check
^
main.go:11:5: Error return value is not checked (errcheck)_= hello("Kyden") // err assign to _
^
X. whitespace
whitespace 是一个 Go 语言的 linter,主要用于检查代码中不必要的空行,即检查函数、条件语句(如 if、for)等开头和结尾的多余空行。
# Install the latest version. (Install it into ./bin/ by default).$ curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s
# Specify installation directory ($(go env GOPATH)/bin/) and version.$ curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s -- -b $(go env GOPATH)/bin [vX.Y.Z]# In alpine linux (as it does not come with curl by default)$ wget -O - -q https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s [vX.Y.Z]
$ curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
reviewdog/reviewdog info checking GitHub for latest tag
reviewdog/reviewdog info found version: 0.20.1 for v0.20.1/Darwin/arm64
reviewdog/reviewdog info installed /Users/kyden/go/bin/reviewdog
如何使用 reviewdog ?
本地使用
1
golangci-lint run ./... 2>&1| reviewdog -f=golangci-lint -reporter=local