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.


3 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.

micheal pan said...

BE SMART AND BECOME RICH IN LESS THAN 3DAYS....It all depends on how fast 
you can be to get the new PROGRAMMED blank ATM card that is capable of
hacking into any ATM machine,anywhere in the world. I got to know about 
this BLANK ATM CARD when I was searching for job online about a month 
ago..It has really changed my life for good and now I can say I'm rich and 
I can never be poor again. The least money I get in a day with it is about 
$50,000.(fifty thousand USD) Every now and then I keeping pumping money 
into my account. Though is illegal,there is no risk of being caught 
,because it has been programmed in such a way that it is not traceable,it 
also has a technique that makes it impossible for the CCTVs to detect 
you..For details on how to get yours today, email the hackers on : (
atmmachinehackers1@gmail.com ). Tell your 
loved once too, and start to live large. That's the simple testimony of how 
my life changed for good...Love you all ...the email address again is ;
atmmachinehackers1@gmail.com