Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using go build but I also see the -test flags

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.

like image 204
Mustafa Avatar asked Mar 09 '23 20:03

Mustafa


1 Answers

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.

like image 142
icza Avatar answered Mar 20 '23 07:03

icza