smrcptr
v1.4.1
不要混合接收器类型。为所有可用方法选择指针或结构类型。
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)Go Wiki https://github.com/golang/go/wiki/codereviewcomments#receiver-type↩
Google Go Sty Style指南https://google.github.io/styleguide/go/decisions#Receiver-Type↩