見出し画像

Golang_golangci-lintでリンターを実行する #475

Goを使ったプロジェクトへジョインし、毎日とても面白く作業させていただいてますが、Goの知見が浅いこともあり日々勉強です。

今回はGoのリンターであるgolangci-lintについてまとめます。

golangci-lintとは

その名の通りGoのリンターで、複数のリンターを統合して使える点が特徴的です。公式で以下のように説明されています。

golangci-lint is a fast Go linters runner.

It runs linters in parallel, uses caching, supports YAML configuration, integrates with all major IDEs, and includes over a hundred linters.

https://pkg.go.dev/github.com/golangci/golangci-lint#section-readme

開発中に少しハマっていたですが、複数のリンターを統合している、というのを理解できていなかったのが要因でした。

基本的な設定と使い方

インストール

homebrewでインストールできます。

$ brew install golangci-lint
$ brew upgrade golangci-lint

設定ファイルの作成

プロジェクトのルートディレクトリに.golangci.ymlという設定ファイルを作成します。
公式(https://golangci-lint.run/usage/configuration/#linters-settings-configuration)から設定項目を引用します。

# Options for analysis running.
run:
  # See the dedicated "run" documentation section.
  option: value
# output configuration options
output:
  # See the dedicated "output" documentation section.
  option: value
# All available settings of specific linters.
linters-settings:
  # See the dedicated "linters-settings" documentation section.
  option: value
linters:
  # See the dedicated "linters" documentation section.
  option: value
issues:
  # See the dedicated "issues" documentation section.
  option: value
severity:
  # See the dedicated "severity" documentation section.
  option: value

詳細は公式で参照可能ですが、後続でlinters-settingslintersについては少し触れます(今回ハマった部分だったので)。

リンターの実行

.golangci.ymlがあるルートディレクトリで以下のコマンドを実行すればOKです。

$ golangci-lint run ./...

実行オプションもあります。-vで詳細な出力を表示でき、--fixで自動修正可能な問題を修正してくれます(何でも自動修正されるわけではない)。

$ golangci-lint run ./... -v --fix


設定ファイルの解説(一部)

公式に詳細は載っていますが、今回特に学びになったところだけメモしておきます。

上記の設定ファイルのうち、linters-settingslintersについてです。

linters-settings

使用する各リンターの設定をデフォルトから変更したい時はここで定義します。例えばコードの複雑度をチェックするgocyclogocognitで、警告する複雑度の閾値を変更したい場合は以下のように書けます(公式からの引用)。

linters-settings:
  gocyclo:
    # Minimal code complexity to report.
    # Default: 30 (but we recommend 10-20)
    min-complexity: 10
  gocognit:
    # Minimal code complexity to report.
    # Default: 30 (but we recommend 10-20)
    min-complexity: 15

以下のコマンドでサポートされているリンターを確認することもできます。

$ golangci-lint help linters

linters

どのリンターを有効にするか、無効にするかを定義できます。全てを無効にしてから特定のリンターを有効にしたり、全てを有効にしてから特定のリンターを無効にしたり、といった設定方法も可能です。

設定方法を公式から引用します(リンターはたくさんあるので減らして記載してます)。

linters:
  # Disable all linters.
  # Default: false
  disable-all: true
  # Enable specific linter
  # https://golangci-lint.run/usage/linters/#enabled-by-default
  enable:
    - asasalint
    - asciicheck
    - bidichk
    - bodyclose
    - canonicalheader
    - gocognit
    - goconst
    - gocritic
    - gocyclo


  # Enable all available linters.
  # Default: false
  enable-all: true
  # Disable specific linter
  # https://golangci-lint.run/usage/linters/#disabled-by-default
  disable:
    - errname
    - errorlint
    - execinquery
    - exhaustive
    - exhaustruct
    - exportloopref


  # Enable presets.
  # https://golangci-lint.run/usage/linters
  # Default: []
  presets:
    - bugs
    - comment
    - complexity
    - error
    - format
    - import
    - metalinter
    - module


  # Enable only fast linters from enabled linters set (first run won't be fast)
  # Default: false
  fast: true


ここまでお読みいただきありがとうございました!!

参考


この記事が気に入ったらサポートをしてみませんか?