dimanche 27 décembre 2020

In Go is 'go build -race main.go' useless?

Taking this example I stole from a website :

package main
import "fmt"

func main() {
    i := 0
    go func() {
        i++ // write
    }()
    fmt.Println(i) // concurrent read
}

Nothing is detected with build :

user@VM:~$ go build -race main.go 
user@VM:~$ 

But, when running it :

user@VM:~$ go run -race main.go 
0
==================
WARNING: DATA RACE
Write at 0x00c00001c0b0 by goroutine 7:
  main.main.func1()
      /home/user/main.go:8 +0x4e

Previous read at 0x00c00001c0b0 by main goroutine:
  main.main()
     /home/user/main.go:10 +0x88

Goroutine 7 (running) created at:
  main.main()
      /home/user/main.go:7 +0x7a
==================
Found 1 data race(s)
exit status 66

So my question is, when is -race used with build better than run or test ?

Aucun commentaire:

Enregistrer un commentaire