Go示例无法运行

huangapple go评论91阅读模式
英文:

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!
}

huangapple
  • 本文由 发表于 2015年3月28日 12:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/29313626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定