2017-02-15 2 views
3

私はmain.gomypkg/...goです。 go build -o main main.goまたはgo install <pkg that has main.go>を使用しています。これには必要なフラグがいくつかあります。しかし、私はまた、テストフラグを参照してください。なぜこうなった?私は何が欠けていますか?go buildを使用していますが、テストフラグも表示されます

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は私のフラグですが、他のものは私のフラグではありません。

答えて

5

ほとんどの場合、flagパッケージのデフォルトフラグセット(flag.FlagSet)が使用されています。そして、それを使用する唯一の人ではないかもしれないことに注意してください。他のパッケージをインポートすると、フラグを登録することもできます。フラグは自分のフラグ(登録したフラグ)と同じように処理されます。このアプリはportフラグを登録していない、と何も

import (
    "flag" 
    _ "testing" 
) 

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

は、この単純な例を参照してください。しかし、多くのフラグを登録する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パッケージのトップレベル関数の代わりにそのメソッドを使用する必要があります。

+0

非常に感謝しています! – Mustafa

関連する問題