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

时间:2017-02-15 08:16:27

标签: testing go build flags

我有main.gomypkg/...go。我使用go build -o main main.gogo 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是我的标志,但正如你所看到的那些不是我的标志。

1 个答案:

答案 0 :(得分:7)

您很可能正在使用flag.FlagSet包的默认标记集(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包的顶级功能。