linehk / gopl

gopl(The Go Programming Language) is a project that contains all the sample code and all exercise answers in the Go Programming Language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

e1.3执行基准测试输出大量 test.paniconexit0 -test.timeout是正常的吗

wei98k opened this issue · comments

这个输出的内容是正常的吗? 如果是正常的话 可以不输出这些内容只输出最后的结果吗?

操作步骤
cd gopl/ch1/exercise1.3

go test -bench=.

输出下面内容

....
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
-test.paniconexit0 -test.timeout=10m0s -test.bench=.
....

这是因为 echo1 等函数里面都用到了 os.Args,用来打印输入的参数,而使用 go test -bench=. 会传入一些默认选项,即 -test.paniconexit0 -test.timeout=10m0s -test.bench=.
临时解决方案可以是,把 echo1 等函数里面的 fmt.Println(s) 都删除掉,这样就只会输出最后的结果了,但是这样就起不到基准测试的作用了。
最终解决方案可能要修改 echo1 这些函数,定义一个特定的命令行选项,传入该命令行的才返回。
@peter-wow 感谢提醒。