英文:
"cannot find package" error on godoc playable examples
问题
我正在尝试在godoc上创建一个可玩的示例。
- 操作系统:OS X Yosemite 10.10.5
- Go版本:go1.7.4 darwin/amd64
测试代码:
$GOPATH/src/hoge/hoge_test.go
package hoge_test
import (
"fmt"
"hoge"
"testing"
)
func ExampleHoge() {
fmt.Println(hoge.Hoge())
// Output:
// hoge!!
}
测试通过:
$ go test hoge
ok hoge 0.011s
Godoc命令:
$ godoc -play -http=:8080
我可以在Web浏览器上看到hoge
包的可玩示例,但是当我点击"Run"运行示例时,出现以下错误:
tmp/sandbox389904218/main.go:5:2: cannot find package "hoge" in any of:
/usr/local/go/src/hoge (from $GOROOT)
/go/src/hoge (from $GOPATH)
我设置了自己的GOPATH,但错误中显示的不是/go
。
我需要怎么运行示例呢?
附加信息
我找到了上述错误的原因。Godoc可玩示例重定向到play.golang.org来运行代码,所以错误中的GOPATH似乎是play.golang.org环境中的GOPATH。
不过,我仍然不确定如何运行自己的包示例...
英文:
I'm trying to make palyable example on godoc.
- OS X Yosemite 10.10.5
- Go: go1.7.4 darwin/amd64
test code:
$GOPATH/src/hoge/hoge_test.go
package hoge_test
import (
"fmt"
"hoge"
"testing"
)
func ExampleHoge() {
fmt.Println(hoge.Hoge())
// Output:
// hoge!!
}
The test passes:
$ go test hoge
ok hoge 0.011s
Godoc
$ godoc -play -http=:8080
I can see the hoge
package's example playground as I expected on a web browser, but an error below occurs when I 'Run' the example.
tmp/sandbox389904218/main.go:5:2: cannot find package "hoge" in any of:
/usr/local/go/src/hoge (from $GOROOT)
/go/src/hoge (from $GOPATH)
I set my own GOPATH, but it's not /go
as shown in the error.
What I need to run examples.
Additional
I've got a reason of the error above.
Godoc playable example redirects to play.golang.org to run the code, so the GOPATH in the error seems to be on the play.golang.org environment.
I'm still not sure how to run my own pkg example though...
答案1
得分: 1
这是因为godoc使用了golang.org/x/tools/playground工具,该工具只是将请求代理到golang.org。除非你的示例函数位于已发布在golang.org上的包中,否则似乎无法运行它们。这是相关的github文件:https://github.com/golang/tools/blob/master/playground/common.go,godoc工具在此处引用了它:https://github.com/golang/tools/blob/master/cmd/godoc/play.go。
英文:
This is because godoc uses the golang.org/x/tools/playground tool, which just proxies requests to golang.org. It does not appear that you can run your example functions unless they are in a package that is published on golang.org. Here's the relevant file in github: https://github.com/golang/tools/blob/master/playground/common.go which is referenced by the godoc tool here: https://github.com/golang/tools/blob/master/cmd/godoc/play.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论