英文:
Go example won't run
问题
我正在尝试向一个包添加一个示例,并通过go test
运行该示例,但是示例从未运行。
例如,查看这个 gist:https://gist.github.com/85469ecc65bb5bb85857
这个 gist 有一个 example_test.go
文件:
package cow_test
import (
cow "gist.github.com/85469ecc65bb5bb85857"
)
func Example() {
cow.Poke()
}
然而,当我运行以下命令时:
# go test -v example_test.go
testing: warning: no tests to run
PASS
ok command-line-arguments 0.002s
然而,stdlib 中的其他包却可以正常工作:
# cd /usr/lib/go/src/errors
# go test -v example_test.go
=== RUN: Example
--- PASS: Example (0.00s)
PASS
ok command-line-arguments 0.002s
我的示例有什么问题?
英文:
I'm trying to add an example to a package, and run the example via go test
, however the example is never run.
For example, see this gist: https://gist.github.com/85469ecc65bb5bb85857
The gist has example_test.go
:
package cow_test
import (
cow "gist.github.com/85469ecc65bb5bb85857"
)
func Example() {
cow.Poke()
}
Yet when I run this:
# go test -v example_test.go
testing: warning: no tests to run
PASS
ok command-line-arguments 0.002s
However other packages from stdlib work just fine:
# cd /usr/lib/go/src/errors
# go test -v example_test.go
=== RUN: Example
--- PASS: Example (0.00s)
PASS
ok command-line-arguments 0.002s
What is wrong with my example?
答案1
得分: 28
从文档中可以看到:
> 没有输出注释的示例函数会被编译但不会被执行。
添加一个输出注释:
func Example() {
junk.Poke()
// Output: MOOOO!
}
英文:
From the documentation:
> Example functions without output comments are compiled but not executed.
Add an output comment:
func Example() {
junk.Poke()
// Output: MOOOO!
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论