【Golang】Linters

Posted by 西维蜀黍 on 2021-07-28, Last Modified on 2022-04-06

Go Focused

  • GolangCI - Open Source SaaS service for running linters on Github pull requests. Free for Open Source.

  • Go Report Card - Go repo report card.

GolangCI

$ brew install golangci-lint

To run golangci-lint execute:

$ golangci-lint run

It’s an equivalent of executing:

$ golangci-lint run ./...

You can choose which directories and files to analyze:

$ golangci-lint run dir1 dir2/... dir3/file1.go

Ref

Code Formatting

  • gofmt - Gofmt formats Go programs. Must have for every project. Don’t forget to use -s flag.
  • goimports - Goimports does everything that gofmt does. Additionally it checks unused imports.

Style and Patterns Checking

  • GoLint - Golint is a linter for Go source code.

  • misspell - Finds commonly misspelled English words

misspell

Correct commonly misspelled English words… quickly.

$ go get -u github.com/client9/misspell/cmd/misspell

# Just list a directory you'd like to check
$ misspell .

Ref https://github.com/client9/misspell

GoLint

$ go get -u golang.org/x/lint/golint

$ golint ./...

Ref

Bugs

  • go vet - Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string. Can check shadowing of variables, but must be enabled explicitly.
  • errcheck - Errcheck is a program for checking for unchecked errors in Go programs.

errcheck

$ go get -u github.com/kisielk/errcheck

# To check all packages beneath the current directory:
$ errcheck ./...

Refer to https://github.com/kisielk/errcheck

Unused Functions

# download from https://github.com/dominikh/go-tools/releases

$ ./staticcheck ./... | grep -v "pb.go"

# show unused functions only
$ staticcheck ./... | grep -v "pb.go" | grep U1000

Linters Helper Tools

  • golangci-lint - Linters Runner for Go. 5x faster than gometalinter. Nice colored output. Can report only new issues. Fewer false-positives. Yaml/toml config.

Reference