This is a creation in Article, where the information may have evolved or changed.
Race detection in Golang
Because the go in Golang is very convenient, plus the function is very easy to hide go.
So a lot of times, when we write a program, we don't know if the program will have any problems in the concurrency scenario.
So in essence, the use of goroutine increases the risk factor of the function on the use of goroutine in the go language. For example a global variable, if we do not add a lock, we write a relatively large project down, we do not know whether this variable will cause multiple Goroutine competition.
This is illustrated by an example from the introducing the Go Race detector in the official website:
package mainimport( "time" "fmt" "math/rand")func main() { start := time.Now() var t *time.Timer t = time.AfterFunc(randomDuration(), func() { fmt.Println(time.Now().Sub(start)) t.Reset(randomDuration()) }) time.Sleep(5 * time.Second)}func randomDuration() time.Duration { return time.Duration(rand.Int63n(1e9))}
This example seems to have no problem, but in fact, time. Afterfunc is an additional goroutine that initiates the timing and execution of func ().
Because of the operation of the T (Timer) in Func (T.reset), the main goroutine also operates on T (T=time. After).
At this time, it is possible to cause two goroutine to compete against the same variable.
This example can be a bit complicated, so let's simplify it by using a simpler example:
package mainimport( "time" "fmt")func main() { a := 1 go func(){ a = 2 }() a = 3 fmt.Println("a is ", a) time.Sleep(2 * time.Second)}
In the example above, looking at the code, we actually see that the go Func trigger Goroutine will modify a.
The master Goroutine also modifies a. But if we run only go run, we may not find much of a problem.
runtime go run race1.goa is 3
Fortunately, Golang introduced the concept of competitive detection after 1.1. We can use Go run-race or go build-race for competitive detection.
The approximate implementation of Golang language is to open multiple goroutine simultaneously to execute the same command, and to record the state of each variable.
If we use race to detect the above program, we will see the output:
runtime go run -race race1.goa is 3==================WARNING: DATA RACEWrite by goroutine 5: main.func·001() /Users/yejianfeng/Documents/workspace/go/src/runtime/race1.go:11 +0x3aPrevious write by main goroutine: main.main() /Users/yejianfeng/Documents/workspace/go/src/runtime/race1.go:13 +0xe7Goroutine 5 (running) created at: main.main() /Users/yejianfeng/Documents/workspace/go/src/runtime/race1.go:12 +0xd7==================Found 1 data race(s)exit status 66
This command outputs the warning, which tells us that Goroutine5 runs to line 11th and main Goroutine to run to 13 rows to trigger the competition.
And Goroutine5 was produced on line 12th.
This way we can see where the program is written in question, based on the analysis of this hint.
Of course this parameter will cause the CPU and memory usage to increase, so it is basically used in the test environment, not in the formal environment to open.