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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件