-
Hi big fan of golangci-lint here! thanks a lot for saving me tons of times. I have question about some hard to find data race error on code. 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)
} By fixing I tried to enable all linters in golangci-lint and many possible linters rules but none of linter catches this data race code. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
Hello, I'm not sure with a static analysis, it is possible to detect this race condition but it can be possible to detect receiver "inconsistency". |
Beta Was this translation helpful? Give feedback.
-
Thanks for sharing the example. I have two comments about this:
|
Beta Was this translation helpful? Give feedback.
Some IDEs handle this rule, but I think there is no existing linter on this topic.