2
0

はじめに

Goでは golangci-lint というリンターにgofmt, goimports, govetなど100以上のリンターが入っている。

インストール

バイナリ

curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.59.1

brew

brew install golangci-lint
brew upgrade golangci-lint

最新リリースがbrew似ない時はbrew tapからインストールする。

brew tap golangci/tap
brew install golangci/tap/golangci-lint

go installでインストールするのは非推奨

その他は、Install | golangci-lint を参考にしてください。

使用方法

バージョン確認

$ golangci-lint --version
golangci-lint has version 1.59.1 built with go1.22.3 from 1a55854a on 2024-06-09T18:08:33Z

実行

以下のコマンドで実行できる。./...は省略可能。

golangci-lint run

go workspaceを使用している場合はgo list -f '{{.Dir}}/...' -m | xargs golangci-lint runとしてください。

ファイルやディレクトリも指定できる。

golangci-lint run dir1 dir2/...
golangci-lint run file1.go

特定のlinterだけチェックする場合は、以下のようにする(以下の例では、errcheckを指定)

golangci-lint run --disable-all -E errcheck

デバッグ

-vをつけると、詳細を出力できる。

$ golangci-lint run -v
INFO golangci-lint has version 1.59.1 built with go1.22.3 from 1a55854a on 2024-06-09T18:08:33Z 
INFO [config_reader] Config search paths: [./ /workspace / /home/vscode] 
      ・・・・・v
INFO File cache stats: 0 entries of total size 0B 
INFO Memory: 3 samples, avg is 30.8MB, max is 38.8MB 
INFO Execution took 150.923458ms

また、環境変数GL_DEBUGを指定して、より詳細なデバッグができる。指定できる値は、logutils.go に記載されています。

GL_DEBUG="loader,gocritic" golangci-lint run

有効なリンターの確認

どのリンターが有効になっているかは、golangci-lint help lintersで確認できる。Enabled by default lintersに現在有効になっているリンターが出力される。Disabled by default lintersに無効なリンターが出力される。

以下は設定

$ golangci-lint help linters
Enabled by default linters:
errcheck: errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
gosimple: Linter for Go source code that specializes in simplifying code [fast: false, auto-fix: false]
govet: Vet examines Go source code and reports suspicious constructs. It is roughly the same as 'go vet' and uses its passes. [fast: false, auto-fix: false]
ineffassign: Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
staticcheck: It's a set of rules from staticcheck. It's not the same thing as the staticcheck binary. The author of staticcheck doesn't support or approve the use of staticcheck as a library inside golangci-lint. [fast: false, auto-fix: false]
unused: Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]

Disabled by default linters:
asasalint: check for pass []any as any in variadic func(...any) [fast: false, auto-fix: false]
        ・・・・・・

ちなみに、以下のリンクからからgolangci-lintで使用可能なリンターの一覧は確認できる。また、deadcodestructcheckなど廃止されたリンターも確認できます。

https://golangci-lint.run/usage/linters/

golangci-lint に搭載されている linter を学ぶ に幾つかのエラーの説明が記載されていたが、内容が古かったので注意してください。

設定ファイル

デフォルトで以下の設定ファイルを探しに行く。

  • .golangci.yml
  • .golangci.yaml
  • .golangci.toml
  • .golangci.json

設定ファイルを指定したい場合は、-cをつける。

以下は、.golangci.tomlの例です。

.golangci.toml
[linters]
disable-all = true
enable = [
  "unparam",
  "govet",
  "gci",
  "typecheck",
  "unused",
  "gosimple",
  "ineffassign",
  "staticcheck",
]

調べてみると、超厳格な場合のベストプラクティス例がありました。(detailsタグ内でgist埋め込めないんですね...)
Golden config for golangci-lint

VSCodeの設定

.vscode/settings.json
{
  "go.lintTool": "golangci-lint",
  "go.lintFlags": ["--config=${workspaceFolder}/.golangci.toml", "--fast"]
}

--fastを設定しないとエディタが落ちる場合があるので注意。
ただし、--fastをつけるとVSCodeのコード上にエラーがたまに出てこないときがあります。

VSCode以外の設定方法は、Integrations を参考にしてください。

Github Action

golangci/golangci-lint-action

以下のように書くと動く。

  - uses: actions/checkout@v4
  - uses: actions/setup-go@v5
    with:
      go-version-file: go.mod
  - name: golangci-lint
    uses: golangci/golangci-lint-action@v6

reviewdog/action-golangci-lint@v2

reviewdogのgolangci-lint版。Lintで落ちた箇所に自動でレビューをしてくれる。

image.png

自分は以下のようにしてます。

  - uses: actions/checkout@v4
  - name: Check lint
    uses: reviewdog/action-golangci-lint@v2
    with:
      go_version_file: go.mod
      github_token: ${{ secrets.github_token }}
      reporter: github-pr-review # PRにコメント
      level: error 
      filter_mode: nofilter # 変更箇所以外でエラーが出てもアノテーションをつけてくれる
      golangci_lint_flags: "--config=.golangci.toml"
      fail_on_error: true  # lintでエラーが出たら失敗にする 

levelをerrorにすることで、マージできないようにすることが可能です。

reviewdog/action-golangci-lint@v2actions/setup-goactions/cacheが入っているので事前に実行する必要はないです。

スライド

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0