Linting Golang projects

Why should I use a lint?

  • Readability
  • Maintainability
  • Help the code review process
  • Avoid (syntax) issues
  • Ensure stiles and standards
  • Following the good practices

Linter for Golang

I’d like to suggest to use:

To Install

# binary will be $(go env GOPATH)/bin/golangci-lint
curl -sSfL | sh -s -- -b $(go env GOPATH)/bin v1.30.0

golangci-lint --version

More info and options:

To Run

Use GitHub Actions to integrate it with your CI

See its demonstration:

How to Configure

Add in the root directory of your project the .golangci.yml file with its rules:

Check it in the lintpoc project:

How to enable it on the CI with GitHub Actions:

Create the file `.github/workflows/golangci-lint.yml` with the following content. It will use the configuration in the root directory and will perform the checks.

name: golangci-lint
      - v*
      - master
    name: lint
    runs-on: ubuntu-latest
      - uses: actions/checkout@v2
      - name: golangci-lint
        uses: golangci/golangci-lint-action@v1
          # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
          version: v1.29

          # Optional: working directory, useful for monorepos
          # working-directory: somedir

          # Optional: golangci-lint command line arguments.
          # args: --issues-exit-code=0

          # Optional: show only new issues if it's a pull request. The default value is `false`.
          # only-new-issues: true

Suggestion: Configure it to allow you check the lint locally via Makefile:

lint: golangci-lint ## Run golint

To install the lint in the bin/ dir of the project:

ifeq (, $(shell which golangci-lint))
	@{ \
	set -e ;\
	curl -sSfL | sh -s -- -b $(shell go env GOPATH)/bin v1.29.0 ;\
GOLANGCI_LINT=$(shell go env GOPATH)/bin/golangci-lint
GOLANGCI_LINT=$(shell which golangci-lint)

**NOTE:** It is NOT a good practice commit binaries. In this way, do not forget to add in the .gitignore:



Use the flag –fix to automatically fix some checks and issues:

golangci-lint run --fix

See that you can setup this helper in your Makefile as well:

lint-fix: golangci-lint ## Run golint fix
	@$(GOLANGCI_LINT) run --fix

How to ignore the false positives and to deal with exceptions:

To ignore specific checks into specific areas of your code :

// nolint:<checkName>


Also, you can perform configurations in the .golangci.yml to ignore directories, files and etc. Also, it can be very specific such as; for the check N in the files A,B,C ignore the errors with “X” see:

Demo Project:

The following project was created to help you with and let you know how it works. Feel free to check:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s