go install github.com/geraldywy/go-refcheck
go-refcheck
reports function receivers where use of large structs are passed by value.
Passing large structs as function receivers generates a copy in memory to work off of. This can create potential issues with the progam's memory heap.
Also, see copyfighter
for a similar linter.
type MyLargeStruct struct {
A string
B int64
C float64
D string
E *bool
}
func (l *MyLargeStruct) Okay() bool {
return *l.E
}
func (l MyLargeStruct) NotOkay() bool {
return *l.E
}
The function NotOkay
is flagged as follows:
$ go-refcheck ./...
$ .../testdata/src/p/p.go:28:9: large struct "MyLargeStruct" passed by value to function receiver
More examples can be found in the testdata folder.
By default, large structs are assumed to be larger than 32 bytes, this value can be toggled with a max
flag.
Eg: To only flag for large structs larger than 64 bytes:
go-refcheck -max 64 ./...