https://github.com/raeperd/recvcheck
Golang linter checks for receiver type consistency
https://github.com/raeperd/recvcheck
Last synced: about 1 year ago
JSON representation
Golang linter checks for receiver type consistency
- Host: GitHub
- URL: https://github.com/raeperd/recvcheck
- Owner: raeperd
- License: mit
- Created: 2024-09-12T14:34:06.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-18T16:35:14.000Z (over 1 year ago)
- Last Synced: 2025-05-04T17:46:23.662Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# recvcheck
[](https://github.com/raeperd/recvcheck/actions/workflows/build.yaml) [](https://goreportcard.com/report/github.com/raeperd/recvcheck) [](https://coveralls.io/github/raeperd/recvcheck?branch=ci-coverall)
Golang linter for check receiver type in method
## Motivation
From [Go Wiki: Go Code Review Comments - The Go Programming Language](https://go.dev/wiki/CodeReviewComments#receiver-type)
> Don’t mix receiver types. Choose either pointers or struct types for all available method
Following code from [Dave Cheney](https://dave.cheney.net/2015/11/18/wednesday-pop-quiz-spot-the-race) causes data race. Could you find it?
This linter does it for you.
```go
package main
import (
"fmt"
"time"
)
type RPC struct {
result int
done chan struct{}
}
func (rpc *RPC) compute() {
time.Sleep(time.Second) // strenuous computation intensifies
rpc.result = 42
close(rpc.done)
}
func (RPC) version() int {
return 1 // never going to need to change this
}
func main() {
rpc := &RPC{done: make(chan struct{})}
go rpc.compute() // kick off computation in the background
version := rpc.version() // grab some other information while we're waiting
<-rpc.done // wait for computation to finish
result := rpc.result
fmt.Printf("RPC computation complete, result: %d, version: %d\n", result, version)
}
```
## References
- [Is there a way to detect following data race code using golangci-lint or other linter?? · golangci/golangci-lint · Discussion #5006](https://github.com/golangci/golangci-lint/discussions/5006)
- [Wednesday pop quiz: spot the race | Dave Cheney](https://dave.cheney.net/2015/11/18/wednesday-pop-quiz-spot-the-race)