dongxingchang9345 2016-12-26 21:14
浏览 136
已采纳

godoc可播放示例上的“找不到包”错误

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条回答 默认 最新

  • dongle7637 2017-02-15 20:36
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错