测试中的标志导致其他测试无法运行

I have a test file that I want to only run when a bit flag is set. I followed the simple example from golang's testing docs:

package mypkg

var myFlagSet = flag.Bool("myflag", false, "")
func TestMain(m *testing.M) {
    flag.Parse()
    if *myFlagSet {
        os.Exit(m.Run())
    }
}

if I run go test ./mypkg -myflag it runs as expected. However, when I run go test ./... -myflag, all other package tests fail:

flag provided but not defined: -myflag

I want to be able to run all tests and not have to worry about parsing this flag in every test file. Is there a way to do this that I'm missing?

When you run test ./... it runs tests for each package as a separate executable, with the same parameters. So if you want to pass a flag to every package, every package has to accept it. You might want to use an env var instead, which can be read by packages that use it and ignored by those that don't.