在godoc可运行示例中出现“无法找到包”错误。

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

"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

huangapple
  • 本文由 发表于 2016年12月27日 05:14:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/41336341.html
匿名

发表评论

匿名网友

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

确定