I'm trying to enable the race detector: https://go.dev/doc/articles/race_detector
Since I can't upload the source code to the test server, how can I enable -race
or compile a binary with -race
enabled?
CodePudding user response:
Add -race
to however you're building now, as stated in the document you've linked to in your question.
For example,
go build -race main.go
CodePudding user response:
The -race
is used to capture the developer's issue in goroutine sync or communication, you can use it to test it by go test -race file.go
, not recommended to use it with binary.
Here is the extract from the same link you have provided
Runtime Overhead
The cost of race detection varies by program, but for a typical program, memory usage may increase by 5-10x and execution time by 2-20x.
The race detector currently allocates an extra 8 bytes per defer and recover statement. Those extra allocations are not recovered until the goroutine exits. This means that if you have a long-running goroutine that is periodically issuing defer and recover calls, the program memory usage may grow without bound. These memory allocations will not show up in the output of runtime.ReadMemStats or runtime/pprof.