Can be used for Go
implementation language code analysis, there are many, such as golint
, gofmt
, misspell
etc. If eleven reference configuration, will be rather cumbersome, so we usually do not use them alone, but the use golangci-lint
.
Official website address: https://github.com/golangci/golangci-lint
golangci-lint
Is an integrated tool, it integrates many static code analysis tools, easy for us to use. By configuring this tool, we can flexibly enable the required code specification checks.
1. Installation
If you want to use it golangci-lint
, you need to install it first. Because golangci-lint
itself is Go
language, so we can install it from the source code, open a terminal and enter the following command to install.
go get github.com/golangci/golangci-lint/cmd/[email protected]
Use this command to install the v1.35.2 version golangci-lint
,
If the installation fails, use the following command:
# binary will be $(go env GOPATH)/bin/golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.35.2
Please refer to https://blog.csdn.net/txl910514/article/details/105880125 if it cannot be installed successfully
wohu@ubuntu:~$ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.35.2
golangci/golangci-lint info checking GitHub for tag 'v1.35.2'
golangci/golangci-lint info found version: 1.35.2 for v1.35.2/linux/amd64
golangci/golangci-lint info installed /home/wohu/gocode/bin/golangci-lint
wohuh@ubuntu:~$ ls go
If the installation still fails, use the following method to download the source code and execute the installation script
cd $GOPATH/src/github.com
git clone https://github.com/golangci/golangci-lint.git
cd golangci-lint/
chmod +x install.sh
sh install.sh -b $(go env GOPATH)/bin v1.38.0
After the installation is complete, enter the following command in the terminal to check whether the installation is successful.
golangci-lint --version
golangci-lint has version 1.35.2 built from 1da5701 on 2021-01-11T02:54:03Z
2. Run
The installation is successful golangci-lint
, the code can be checked using it, the sample code
package main
import "os"
const name string = "wohu"
func main() {
os.Mkdir("demo_test", 0755)
}
Run the command:
wohu@ubuntu:~/gocode/src$ golangci-lint run demo.go
demo.go:5:7: `name` is unused (deadcode)
const name string = "wohu"
^
demo.go:7:13: Error return value of `os.Mkdir` is not checked (errcheck)
os.Mkdir("demo_test", 0755)
^
It can be seen from the code inspection results that both code specification issues have been detected. Once the problems are detected, you can fix them to make the code more compliant.
3. Configuration
golangci-lint
The configuration of is more flexible, for example, you can customize which ones to enable linter
. golangci-lint
The ones linter
that are enabled by default include these:
- deadcode-Dead code check
- errcheck-Returns whether the error is checked
- gosimple-check if the code can be simplified
- govet-code suspicious checks, such as inconsistencies in format strings and types
- ineffassign-check for unused code
- staticcheck-static analysis check
- structcheck-find unused structure fields
- typecheck-type check
- unused-unused code check
- varcheck-unused global variables and constant checks
Tip:
golangci-lint
supports morelinter
, you can enter in a terminalgolangci-lint linters
command to view, and you can see everylinter
description.
If you want to modify enabled by default linter
, you need to golangci-lint
configure. That is a new name in the project root directory for .golangci.yml
the file, which is the golangci-lint
configuration file. golangci-lint
It will be used automatically when running code specification checks . I can only assume that enable unused
checking, you can configure:
.golangci.yml
linters:
disable-all: true
enable:
- unused
In multiplayer team collaborative development, there is a fixed golangci-lint
version is very important, so we can examine the code based on the same criteria. To configure the golangci-lint
use of relatively simple version, add the following code to the configuration file:
service:
golangci-lint-version: 1.32.2 # use the fixed version to not introduce new linters unexpectedly
In addition, you can also enabled for each linter
configured, for example, to set the language for the spell checker US
, you can use the following code settings:
linters-settings:
misspell:
locale: US
golangci-lint
There are many configurations, and you can configure it flexibly. About golangci-lint
more configuration can refer to the official documentation, and here I give a common configuration, code is as follows:
Configuration file .golangci.yml
linters-settings:
golint:
min-confidence: 0
misspell:
locale: US
linters:
disable-all: true
enable:
- typecheck
- goimports
- misspell
- govet
- golint
- ineffassign
- gosimple
- deadcode
- structcheck
- unused
- errcheck
service:
golangci-lint-version: 1.32.2 # use the fixed version to not introduce new linters unexpectedly
4. Integration into CI
The code checks must be integrated into the CI
process, the effect will be better, so when developers submit code, CI
it will automatically check the code, to identify problems and correct.
Whether you are using Jenkins
, or Gitlab CI
, or Github Action
, you can Makefile
run the way golangci-lint
. Now I create a project in the root directory of Makefile
the file, and add the following code:
getdeps:
@mkdir -p ${GOPATH}/bin
@which golangci-lint 1>/dev/null || (echo "Installing golangci-lint" && go get github.com/golangci/golangci-lint/cmd/[email protected])
lint:
@echo "Running $@ check"
@GO111MODULE=on ${GOPATH}/bin/golangci-lint cache clean
@GO111MODULE=on ${GOPATH}/bin/golangci-lint run --timeout=5m --config ./.golangci.yml
verifiers: getdeps lint