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

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

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型