LoginSignup
1
0

More than 1 year has passed since last update.

備忘録。

構造体のフィールドが全部初期化されているかどうかをチェックしたい場合はexhaustivestructが便利。
exhaustivestructを有効化した上でstruct-patternsに構造体が Path#Match に合致するように記載する。
struct-patternsはパッケージ名も含められるので別パッケージで同名の構造体があっても対応できる。

ハマったポイントとして、struct-patternsに指定するのはチェックしたい構造体が定義されているパッケージおよび構造体名を指定する必要がある点。
勘違いしてチェックしたい構造体が呼び出されているパッケージ名を指定しようとしていた。

package exampleA

type Target {
  Hoge string
}

package main

func main() {
  t := Target{}
}

上記の場合、struct-patternsには exampleA.Target を指定する。

exhaustivestructの使い道としては自動生成される構造体の呼び出し元で定義漏れがないかをチェックする場合
例えばOpenAPIやProtocolBufferを使って構造体を自動生成している場合とかに便利。

参考

https://github.com/mbilski/exhaustivestruct
https://golangci-lint.run/usage/linters/#exhaustivestruct
https://pkg.go.dev/path#Match

1
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
1
0