Open2

GoのStaticcheckについて調査

setsunachansetsunachan

公式ドキュメントを読み進めていく

setsunachansetsunachan

Outline

Staticcehckは、静的解析を行うリンターである

Getting started

Go 1.17から、Staticcheckをインストールする最も簡単な方法は、go install honnef.co/go/toolss/cmd/staticcheck@latestを実行することです。これで最新バージョンのStaticcheckが$GOPATH/binにインストールされます。

Running Staticcheck

Command-line interface

staticcheck ./...で実行する

explainオプション

チェック内容の詳細を出力

package main

import "fmt"

func main() {
	str := fmt.Sprintf("Hello, World")
	fmt.Println(str)
}
staticcheck -explain S1039 ./...
Unnecessary use of fmt.Sprint

Calling fmt.Sprint with a single string argument is unnecessary
and identical to using the string directly.

Available since
    2020.1

Online documentation
    https://staticcheck.io/docs/checks#S1039

一度staticcheckを実行した後に、S1039のような識別子を指定する形なのか
なぜそのエラーが吐かれているか詳細を知りたい時に使えそう

goオプション

staticcheck -go 1.0 ./... のような形で、動作するGoのバージョンを指定することが可能
デフォルトでは、go.modのgoディレクティブに指定されているバージョンで実行される

Continuous integration

Github Actionsのサンプルコードあり↓

https://staticcheck.dev/docs/running-staticcheck/ci/github-actions/

マーケットプレイスで公開されている以下のワークフローを使えば簡単にGithub Actions上でstaticcheckを実行できる
https://github.com/marketplace/actions/staticcheck

普通にGoの実行環境を用意して、staticcheckコマンドを実行するだけでも○