2017-02-15 71 views
3

我有一個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和端口是我的標誌,但正如你所看到的其他人不是我的標誌。

回答

5

很可能您正在使用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包的頂層函數。

+0

非常感謝,謝謝! – Mustafa

相關問題