Monday 15 June 2020

Go Linters in VS Code

If you are using VS Code for Go development, you have probably installed Go for Visual Studio Code extension. It allows configuring a linter of choice by going to settings (CTRL+,), typing go.lintTool and selecting a desired linter:



golint


golint is a default linter. To run it across all files in the project, execute this from the project's root directory:

$ golint ./...

For each issue detected, golint's output contains source code file name, line and column numbers and the linter message.

When I run golint on one of my projects that I used for benchmark, it found 9 issues.



Staticcheck


Staticcheck is an advanced linter. VS Code Go extension can install it automatically if we select staticcheck as go.lintTool value and opt to install it in the popup notification that will apper upon selection:


Installation log can be verified in the output window:


To run it across all files in the project, execute this from the project's root directory:

$ staticcheck ./...

Similar to golinit, for each issue detected the output contains source code file name, line and column numbers and the linter message.

When I run Staticcheck on my benchmark project, it found 57 issues.

Static check's repo is at https://github.com/dominikh/go-tools and shows active development. At the time of writing has 72 watchers, 3.2k stars and 199 forks.

revive


revive is another Go linting tool which is listed among other linters in the default linters list in go.lintTools:



Installation output log:




To run it across all files in the project, execute this from the project's root directory:

$ revive ./...

Similar to golinit, for each issue detected the output contains source code file name, line and column numbers and the linter message.

When I run revive on my benchmark project, it found 11 issues.

Revive repo is at https://github.com/mgechev/revive and shows active development. At the time of writing has 29 watchers, 2.3k stars and 107 forks.


golangci-lint


golangci-lint is a Go linters aggregator which currently includes 48 linters. Some are enabled by default (e.g. staticcheck) and some are not. There is no need to install all linters, this tool does it all, just select golangci-lint as go.lintTool value:




Installation log output:



To run it across all files in the project, execute this from the project's root directory:

$ golangci-lint run

The output is more verbose, in addition to source code file name, line and column numbers, for each issue detected golangci-lint also prints that code line with a caret that shows exact character in line for which the linter message is issued. All messages are coloured.

When I run golangci-lint on my benchmark project, it found the same number of issues as Staticcheck. When compared the output, I found that issues reported were mostly the same as those from Staticcheck but were reported as the catch of some other enabled linters (e.g. gosimple, govet, deadcheck, unused etc...).

golangci-lint repo is at https://github.com/golangci/golangci-lint and shows active development. At the time of writing has 77 watchers, 5.5k stars and 495 forks.


Conclusion


Given the more complete and verbose output and larger community around the project, I would opt for using golangci-lint as the preferred Go linting tool.


2 comments:

Amit Sarkar said...

Any suggestions for JavaScript linters?

Bojan Komazec said...

@Amit Sarkar I've been using ESLint (https://eslint.org/) for JS and am quite happy with it.