doushi1964 2017-02-15 08:16
浏览 132
已采纳

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

I have a main.go and mypkg/...go. I use go build -o main main.go or go install <pkg that has main.go> and which has some flags I require. But I also see the test flags. Why is this happening? What am I missing?

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 and port are my flags, but as you can see the others are not my flags.

  • 写回答

1条回答 默认 最新

  • douwengzao5790 2017-02-15 08:34
    关注

    Most likely you're using the default flag set (flag.FlagSet) of the flag package. And note that you may not be the only one using it. If you import other packages, they might also register flags, which will be processed just like your own flags (flags you registered).

    See this simple example:

    import (
        "flag"
        _ "testing"
    )
    
    func main() {
        flag.Int("port", 80, "port to use")
        flag.Parse()
    }
    

    This app registers a port flag, and nothing else. But it also imports the testing package which registers a lot of flags.

    Running it with the -h command line argument, the output is:

      -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
    

    If you don't want your flags to be mixed with flags of other packages, create and use your own flag.FlagSet by calling flag.NewFlagSet(), but then of course you have to use its methods instead of the top-level functions of the flag package.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化