Testing 使用go build,但我也看到了-test标志

Testing 使用go build,但我也看到了-test标志,testing,go,build,flags,Testing,Go,Build,Flags,我有一个main.go和mypkg/…go。我使用的是go build-o main.go或go install,其中有一些我需要的标志。但我也看到了测试标志。为什么会这样?我错过了什么 Usage of ./main: -docker string Docker API Path, defaults to local (default "unix:///var/run/docker.sock") -httptest.serve string if non

我有一个
main.go
mypkg/…go
。我使用的是
go build-o main.go
go install
,其中有一些我需要的标志。但我也看到了测试标志。为什么会这样?我错过了什么

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是我的标志,但正如您所看到的,其他标志不是我的标志。

很可能您使用的是包的默认标志集()。请注意,您可能不是唯一使用它的人。如果导入其他包,它们也可能注册标志,这些标志将像您自己的标志(注册的标志)一样进行处理

请参见以下简单示例:

import (
    "flag"
    _ "testing"
)

func main() {
    flag.Int("port", 80, "port to use")
    flag.Parse()
}
此应用程序注册了一个
端口
标志,而不注册其他标志。但它也会导入注册了很多标志的包

使用
-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
如果您不希望您的标志与其他包的标志混合,请通过调用创建并使用您自己的
标志.FlagSet
,但当然您必须使用其方法,而不是
标志
包的顶级函数