我有一个main.go和mypkg/...go。我使用go build -o main main.go或go install <pkg that has main.go>并且其中有一些我需要的标志。但是我也看到了测试标志。为什么会这样呢?我想念什么?
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了flag程序包的默认标志集()。请注意,您可能不是唯一使用它的人。如果导入其他软件包,它们也可能会注册标志,这些标志将像您自己的标志(已注册的标志)一样进行处理。
flag.FlagSet
flag
看这个简单的例子:
import ( "flag" _ "testing" ) func main() { flag.Int("port", 80, "port to use") flag.Parse() }
这个程序注册一个port标志,别无其他。但是它也导入了testing注册很多标志的包。
port
testing
使用-h命令行参数运行它,输出为:
-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.FlagSet通过调用来创建和使用自己的标志flag.NewFlagSet(),但是当然您必须使用其方法而不是flag程序包的顶级功能。
flag.NewFlagSet()