英文:
Using go build but I also see the -test flags
问题
我有一个 main.go
和 mypkg/...go
文件。我使用 go build -o main main.go
或 go install <具有 main.go 的包>
命令,并且其中包含一些我需要的标志。但是我也看到了测试标志。为什么会这样?我漏掉了什么?
./main 的用法:
-docker string
Docker API 路径,默认为本地 (默认值为 "unix:///var/run/docker.sock")
-httptest.serve string
如果非空,httptest.NewServer 将在此地址上提供服务并阻塞
-port int
默认监听的端口 (默认值为 8000)
-test.bench string
选择要运行的基准测试的每个路径组件的正则表达式
-test.benchmem
打印基准测试的内存分配
-test.benchtime duration
每个基准测试的大致运行时间 (默认值为 1s)
-test.blockprofile string
在执行后将 goroutine 阻塞分析写入指定的文件
-test.blockprofilerate int
如果 >= 0,则调用 runtime.SetBlockProfileRate() (默认值为 1)
dockerPath 和 port 是我的标志,但是你可以看到其他的标志不是我的标志。
英文:
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
得分: 7
很可能你正在使用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
包的顶级函数。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论