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↩