Open In App

How to Install golangci-lint?

Last Updated : 02 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Golang which is also known as Go is just an open-source programming language that is used to develop web apps and network applications, etc. It is a high-level programming language and is statically typed and compiled.

Golangci-lint is just a fast linter for Go. Now, a linter is a tool that inspects code files using a set of rules that identify issues that lead to misunderstanding, result in unexpected outcomes, or make the code harder to read. Additionally, linter can aid in identifying potential defects in the code, such as undeclared variable assignment, which can result in runtime errors, retrieving the value from a global variable, which complicates debugging, and other issues.

Golangci-lint employs caching, enables yaml configuration, runs multiple linters in parallel, integrates with all major IDEs, and comes with dozens of linters.

So, despite the normal advantages of linter, golangci-linter also improves efficiency and speed with parallel linters and thus increases performance.

How to Install Go?

Now, to install golangci-lint a prerequisite is to install “Go” first. Installing Go in Ubuntu is just a matter of a command.

sudo apt install golang-go

install-go.jpg

Installing GO

To verify if Go is installed properly, we can check the version with this command

go version

Screenshot-from-2023-06-17-16-38-14.png

GO version

How to Install Golangcli-lint?

Since we have Go installed now, we can proceed toward installing golangci-lint. In Ubuntu we can install golangci-lint with snap with just one command

sudo snap install golangci-lint

Screenshot-from-2023-06-17-16-47-18.png

Intsall Golangcli-lint

Now, since we are installing through snap we need to ensure that /snap/bin is included in our PATH environment variable. To do so we can run this command

export PATH=$PATH:/snap/bin

Screenshot-from-2023-06-17-16-50-58.png

PATH env variable

To verify installation check the version of the same

golangci-lint –version

Screenshot-from-2023-06-17-16-51-56.png

Golang-cli version

And that is it, we have successfully installed golangci-lint. We can now use it to lint our Go code and enforce code quality standards.

How to Use golangci-lint?

To first view all the linters available i.e enabled/disabled by golangci-lint we can run this command

golangci-lint help linters

helper.jpg

Enabled Linters

Now, if we run golangci-lint for some project/directory it will use the enabled linters. To run the golangci-lint for your Golang project directory, run the following commands

go get ./…

golangci-lint run

run.jpg

Running golangci-lint

We can also run golangci-lint giving multiple directories and file names.

We can also configure how the golangci-lint with a yaml file “.golangci.yml” with various attributes like which linters to enable/disable, the complexity of the linters, etc. Then when we run it the updated config will be used.

To apply the configuration with our yaml file, use this command

golangci-lint config > .golangci.yml

Conclusion

In this article, we covered the process of installing Go first as a prerequisite. We then covered installation of golangci-lint and finally how can it be configured specifically and then be used in our Go programs to lint our code. By having golangci-lint at our disposal, We can ensure your code is top-notch and easy to understand. With Go and golangci-lint, maintaining great code quality and catching potential issues becomes an integral part of our coding journey.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads