【问题标题】:Using go build but I also see the -test flags使用 go build 但我也看到了 -test 标志
【发布时间】:2017-07-03 18:59:55
【问题描述】:

我有一个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 是我的标志,但你可以看到其他的不是我的标志。

【问题讨论】:

    标签: testing go build flags


    【解决方案1】:

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

    看这个简单的例子:

    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 包的功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 2021-10-19
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多