Go study notes (73)-Go static code analysis tool golangci-lint

Can be used for Goimplementation language code analysis, there are many, such as golint, gofmt, misspelletc. 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-lintIs 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-lintitself is Golanguage, 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-lintThe configuration of is more flexible, for example, you can customize which ones to enable linter. golangci-lintThe ones linterthat 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-lintsupports more linter, you can enter in a terminal golangci-lint linterscommand to view, and you can see every linterdescription.

If you want to modify enabled by default linter, you need to golangci-lintconfigure. That is a new name in the project root directory for .golangci.ymlthe file, which is the golangci-lintconfiguration file. golangci-lintIt will be used automatically when running code specification checks . I can only assume that enable unusedchecking, you can configure:

.golangci.yml

linters:
  disable-all: true
  enable:
    - unused

In multiplayer team collaborative development, there is a fixed golangci-lintversion is very important, so we can examine the code based on the same criteria. To configure the golangci-lintuse 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 linterconfigured, for example, to set the language for the spell checker US, you can use the following code settings:

linters-settings:
  misspell:
    locale: US

golangci-lintThere are many configurations, and you can configure it flexibly. About golangci-lintmore 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 CIprocess, the effect will be better, so when developers submit code, CIit will automatically check the code, to identify problems and correct.

Whether you are using Jenkins, or Gitlab CI, or Github Action, you can Makefilerun the way golangci-lint. Now I create a project in the root directory of Makefilethe 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

Guess you like

Origin blog.csdn.net/wohu1104/article/details/113751501