m.go:
package main
import (
"flag"
)
var (
fooFlag = flag.String("foo", "", "this should be ok")
foo = flag.Lookup("foo")
barFlag = flag.String("bar", "", "this should be also ok, but is "+notOK()+".")
bar = flag.Lookup("bar")
)
func notOK() string {
return "not OK"
}
m_test.go:
package main
import (
"testing"
)
func TestFoo(t *testing.T) {
if foo == nil {
t.Fatal()
}
}
func TestBar(t *testing.T) {
if bar == nil {
t.Fatal()
}
}
When run with go test, this passes. When run with go test -cover it fails:
--- FAIL: TestBar (0.00s)
m_test.go:15:
FAIL
main coverage: 100.0% of statements
exit status 1
FAIL example.com 0.003s
The variables have no initialization dependencies visible to the compiler, so the order of initialization rules say that they should be initialized in declaration order. That is what happens with go test, but it appears to not happen with go test -cover.
This test passed with go test -cover in versions of Go before Go 1.13.
CC @golang/runtime @thanm
m.go:
m_test.go:
When run with
go test, this passes. When run withgo test -coverit fails:The variables have no initialization dependencies visible to the compiler, so the order of initialization rules say that they should be initialized in declaration order. That is what happens with
go test, but it appears to not happen withgo test -cover.This test passed with
go test -coverin versions of Go before Go 1.13.CC @golang/runtime @thanm