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 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿