Golang Lint: Install Golint and Usage Examples

Golang Golint Installation and Examples


golang lintGolint is a useful Go linting tool that prints out coding style mistakes. While it’s not perfect, it presents suggestions that follow many of the items in Effective Go and the CodeReviewComments wiki.

Installing and using golint is simple but can be frustrating if a step is skipped during installation or the environment is not ready. This article will cover installation and usage examples.

Overview

Install Golint

To install golint, first ensure that your local GOPATH and GOBIN are fully setup by using the go env command. You should expect to see the respective path that you have set. If not, set them up accordingly.

Next, ensure your GOPATH and GOBIN directories are in your ~/.bash_profile file. If not, add them by referencing the lines below as an example.

Now, activate your ~/.bash_profile with the source command.

Then, install the package by using the go get -u command. The -u flag will update the packages and it’s respective dependencies.

Finally, verify that the binary is in place.

Golint Examples

The golint tool can be used from the command-line or integrated with an IDE. The examples below will use the golint via command-line.

Single file usage:

Multiple file usage:

Directory usage:

Golint Ignore Line

Unfortunately, according to the golint README, the package “…will not be adding pragmas or other knobs to suppress specific warnings…“. If you need to ignore certain lines or rules, you can alternatively use the gometalinter package.

See also:
Go Column Print Format
Golang Command Line Flags: Input Arguments
Golang SSH Client: Crypto & Goexpect Examples
Go IP Address Manipulation: Parse CIDR & Net IP
Golang Regular Expression Match: Regexp Syntax Tutorial