smrcptr
v1.4.1
受信機の種類を混ぜないでください。利用可能なすべての方法に対して、ポインターまたはstructタイプのいずれかを選択します。
GOには、複雑でバグにつながる可能性のある値受信機またはメソッドレシーバーを自動的に選択する方法に関するルールがあります。したがって、それは一般的なスタイルの推奨事項1 2です。
go install github.com/nikolaydubina/smrcptr@latest type Pancake struct {}
func NewPancake () Pancake { return Pancake {} }
func ( s * Pancake ) Fry () {}
func ( s Pancake ) Bake () {}$ smrcptr ./...
/pancake.go:12:1: Pancake.Fry uses pointer
/pancake.go:10:1: Pancake.NewPancake uses value
/pancake.go:14:1: Pancake.Bake uses value 2022-11-30の時点で、ポインターと値メソッドレシーバーが混合されていることは検出されません。最も関連性の高いアナライザーST1016 、メソッドレシーバーの名前のみをチェックします。
$ staticcheck -checks ST1016 ./...
main.go:9:18: methods on the same type should have the same receiver name (seen 1x " v " , 2x " s " ) (ST1016)すべてのアナライザーを使用しても検出されません。
staticcheck -checks all ./...
main.go:9:18: methods on the same type should have the same receiver name (seen 1x " v " , 2x " s " ) (ST1016)wiki https://github.com/golang/go/wiki/codereviewcomments#receiver-type↩
Google Go Style Guide https://google.github.io/styleguide/go/decisionss#receiver-type↩