Skip to content

Commit 35e7ac1

Browse files
committed
docs: Update docs with examples
1 parent 0e08ef2 commit 35e7ac1

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

README.md

+42-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,50 @@
22
Golang linter for check receiver type in method (WIP)
33

44
## Motivtation
5+
From [Go Wiki: Go Code Review Comments - The Go Programming Language](https://go.dev/wiki/CodeReviewComments#receiver-type)
6+
> Don’t mix receiver types. Choose either pointers or struct types for all available method
7+
8+
Following code from [Dave Chenney](https://dave.cheney.net/2015/11/18/wednesday-pop-quiz-spot-the-race) causes data race. Could you find it?
9+
This linter does it for you.
10+
11+
```go
12+
package main
13+
14+
import (
15+
"fmt"
16+
"time"
17+
)
18+
19+
type RPC struct {
20+
result int
21+
done chan struct{}
22+
}
23+
24+
func (rpc *RPC) compute() {
25+
time.Sleep(time.Second) // strenuous computation intensifies
26+
rpc.result = 42
27+
close(rpc.done)
28+
}
29+
30+
func (RPC) version() int {
31+
return 1 // never going to need to change this
32+
}
33+
34+
func main() {
35+
rpc := &RPC{done: make(chan struct{})}
36+
37+
go rpc.compute() // kick off computation in the background
38+
version := rpc.version() // grab some other information while we're waiting
39+
<-rpc.done // wait for computation to finish
40+
result := rpc.result
41+
42+
fmt.Printf("RPC computation complete, result: %d, version: %d\n", result, version)
43+
}
44+
```
45+
46+
## References
547
- [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)
648
- [Wednesday pop quiz: spot the race | Dave Cheney](https://dave.cheney.net/2015/11/18/wednesday-pop-quiz-spot-the-race)
749

8-
From [Go Wiki: Go Code Review Comments - The Go Programming Language](https://go.dev/wiki/CodeReviewComments#receiver-type)
9-
> Don’t mix receiver types. Choose either pointers or struct types for all available method
1050

1151

0 commit comments

Comments
 (0)