使用 go build 但我也看到了 -test 标志
Using go build but I also see the -test flags
我有一个 main.go
和 mypkg/...go
。我使用 go build -o main main.go
或 go install <pkg that has main.go>
并且其中有一些我需要的标志。但我也看到了测试标志。为什么会这样?我错过了什么?
Usage of ./main:
-docker string
Docker API Path, defaults to local (default "unix:///var/run/docker.sock")
-httptest.serve string
if non-empty, httptest.NewServer serves on this address and blocks
-port int
The default port to listen (default 8000)
-test.bench string
regular expression per path component to select benchmarks to run
-test.benchmem
print memory allocations for benchmarks
-test.benchtime duration
approximate run time for each benchmark (default 1s)
-test.blockprofile string
write a goroutine blocking profile to the named file after execution
-test.blockprofilerate int
if >= 0, calls runtime.SetBlockProfileRate() (default 1)
dockerPath 和 port 是我的标志,但正如你所见,其他不是我的标志。
很可能您使用的是默认标志集(flag.FlagSet
) of the flag
包。请注意,您可能不是唯一使用它的人。如果您导入其他包,它们也可能会注册标志,这将就像您自己的标志(您注册的标志)一样被处理。
看这个简单的例子:
import (
"flag"
_ "testing"
)
func main() {
flag.Int("port", 80, "port to use")
flag.Parse()
}
此应用注册了一个 port
标志,仅此而已。但它也导入了 testing
包,其中注册了很多标志。
运行 它与 -h
命令行参数,输出是:
-port int
port to use (default 80)
-test.bench string
regular expression per path component to select benchmarks to run
-test.benchmem
print memory allocations for benchmarks
-test.benchtime duration
approximate run time for each benchmark (default 1s)
-test.blockprofile string
write a goroutine blocking profile to the named file after execution
-test.blockprofilerate int
if >= 0, calls runtime.SetBlockProfileRate() (default 1)
-test.count n
run tests and benchmarks n times (default 1)
-test.coverprofile string
write a coverage profile to the named file after execution
-test.cpu string
comma-separated list of number of CPUs to use for each test
-test.cpuprofile string
write a cpu profile to the named file during execution
-test.memprofile string
write a memory profile to the named file after execution
-test.memprofilerate int
if >=0, sets runtime.MemProfileRate
-test.outputdir string
directory in which to write profiles
-test.parallel int
maximum test parallelism (default 4)
-test.run string
regular expression to select tests and examples to run
-test.short
run smaller test suite to save time
-test.timeout duration
if positive, sets an aggregate time limit for all tests
-test.trace string
write an execution trace to the named file after execution
-test.v
verbose: print additional output
exit status 2
如果您不希望您的标志与其他包的标志混合,请通过调用 flag.NewFlagSet()
创建并使用您自己的 flag.FlagSet
,当然您必须使用它的方法而不是 flag
包的 top-level 函数。
我有一个 main.go
和 mypkg/...go
。我使用 go build -o main main.go
或 go install <pkg that has main.go>
并且其中有一些我需要的标志。但我也看到了测试标志。为什么会这样?我错过了什么?
Usage of ./main:
-docker string
Docker API Path, defaults to local (default "unix:///var/run/docker.sock")
-httptest.serve string
if non-empty, httptest.NewServer serves on this address and blocks
-port int
The default port to listen (default 8000)
-test.bench string
regular expression per path component to select benchmarks to run
-test.benchmem
print memory allocations for benchmarks
-test.benchtime duration
approximate run time for each benchmark (default 1s)
-test.blockprofile string
write a goroutine blocking profile to the named file after execution
-test.blockprofilerate int
if >= 0, calls runtime.SetBlockProfileRate() (default 1)
dockerPath 和 port 是我的标志,但正如你所见,其他不是我的标志。
很可能您使用的是默认标志集(flag.FlagSet
) of the flag
包。请注意,您可能不是唯一使用它的人。如果您导入其他包,它们也可能会注册标志,这将就像您自己的标志(您注册的标志)一样被处理。
看这个简单的例子:
import (
"flag"
_ "testing"
)
func main() {
flag.Int("port", 80, "port to use")
flag.Parse()
}
此应用注册了一个 port
标志,仅此而已。但它也导入了 testing
包,其中注册了很多标志。
运行 它与 -h
命令行参数,输出是:
-port int
port to use (default 80)
-test.bench string
regular expression per path component to select benchmarks to run
-test.benchmem
print memory allocations for benchmarks
-test.benchtime duration
approximate run time for each benchmark (default 1s)
-test.blockprofile string
write a goroutine blocking profile to the named file after execution
-test.blockprofilerate int
if >= 0, calls runtime.SetBlockProfileRate() (default 1)
-test.count n
run tests and benchmarks n times (default 1)
-test.coverprofile string
write a coverage profile to the named file after execution
-test.cpu string
comma-separated list of number of CPUs to use for each test
-test.cpuprofile string
write a cpu profile to the named file during execution
-test.memprofile string
write a memory profile to the named file after execution
-test.memprofilerate int
if >=0, sets runtime.MemProfileRate
-test.outputdir string
directory in which to write profiles
-test.parallel int
maximum test parallelism (default 4)
-test.run string
regular expression to select tests and examples to run
-test.short
run smaller test suite to save time
-test.timeout duration
if positive, sets an aggregate time limit for all tests
-test.trace string
write an execution trace to the named file after execution
-test.v
verbose: print additional output
exit status 2
如果您不希望您的标志与其他包的标志混合,请通过调用 flag.NewFlagSet()
创建并使用您自己的 flag.FlagSet
,当然您必须使用它的方法而不是 flag
包的 top-level 函数。