Golang 中有没有办法在尝试执行`go test ./... -v` 时设置标志

Is there a way in Golang to set flags when trying to execute `go test ./... -v`

我需要执行类似 go test ./... -v -args -name1 val1 的操作 但是与 go test ... 一起工作的东西似乎与 go test ./...

一起工作

Go 测试框架使用全局 flag.(*FlagSet) instance. Any flags created in test files are available from the commands line. Positional arguments that aren't consumed by the test framework are available via flag.Args()(和 flag.Argflag.NArg)。位置参数需要 -- 在命令行上将它们分开。

例如:

package testflag

import (
    "flag"
    "testing"
)

var value = flag.String("value", "", "Test value to log")

func TestFlagLog(t *testing.T) {
    t.Logf("Value = %q", *value)
    t.Logf("Args = %q", flag.Args())
}

假设以上测试在几个目录testflagtestflag/atestflag/b、运行 go test -v ./... -value bar -- some thing输出:

=== RUN   TestFlagLog
    testflag_test.go:11: Value = "bar"
    testflag_test.go:12: Args = ["some" "thing"]
--- PASS: TestFlagLog (0.00s)
PASS
ok          testflag        0.002s
=== RUN   TestFlagLog
    testflag_test.go:11: Value = "bar"
    testflag_test.go:12: Args = ["some" "thing"]
--- PASS: TestFlagLog (0.00s)
PASS
ok          testflag/a      0.001s
=== RUN   TestFlagLog
    testflag_test.go:11: Value = "bar"
    testflag_test.go:12: Args = ["some" "thing"]
--- PASS: TestFlagLog (0.00s)
PASS
ok          testflag/b      0.002s