简单的godoc Hello world

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

SIMPLE godoc Hello world

问题

尝试在一个简单的、扁平的代码文件夹上提供godoc服务。在线文档没有解释如何实现这个简单的任务。

所以,创建这个简单的结构,

/tmp/testgodoc$ tree
.
└── src
    ├── main  (只是二进制文件)
    └── main.go

1 directory, 2 files

其中main.go只是简单的:

/tmp/testgodoc$ cat src/main.go
// Hello godoc
package main

import "fmt"

// Say Hello
func main() {
    fmt.Println("Hello")
}

无论在GOPATH模式还是模块模式下运行,打开浏览器中的localhost:6060都不能得到预期的结果,即对当前文件夹进行文档化。

在模块模式下运行会得到以下输出和结果:

/tmp/testgodoc$ ~/go/bin/godoc  -goroot=. -http=:6060
using module mode; GOMOD=/dev/null
(when Ctrl-C:) cannot find package "." in:
        /src/main
^C

简单的godoc Hello world

而在GOPATH模式下运行似乎指向了本地的标准库:

/tmp/testgodoc$ GO111MODULE=off ~/go/bin/godoc  -goroot=. -http=:6060
using GOPATH mode
^C

简单的godoc Hello world

英文:

Trying to serve a godoc on a simple, flat code folder. The online docs do not explain how to achieve this SIMPLE task.

So, creating this simple structure,

/tmp/testgodoc$ tree
.
└── src
    ├── main  (just the binary)
    └── main.go

1 directory, 2 files

where main.go is simply

/tmp/testgodoc$ cat src/main.go
// Hello godoc
package main

import "fmt"

// Say Hello
func main() {
    fmt.Println("Hello")
}

When running either in GOPATH or module modes, opening localhost:6060 in a browser does not give the expected result of documenting current folder.

Running in module mode give this output and result:

/tmp/testgodoc$ ~/go/bin/godoc  -goroot=. -http=:6060
using module mode; GOMOD=/dev/null
(when Ctrl-C:) cannot find package "." in:
        /src/main
^C

简单的godoc Hello world

And running in GOPATH mode seems to point to the local standard library:

/tmp/testgodoc$ GO111MODULE=off ~/go/bin/godoc  -goroot=. -http=:6060
using GOPATH mode
^C

简单的godoc Hello world

答案1

得分: 1

你应该将主要的包放入一个子目录中,可能像这样:

~/go/src/testGoDoc$ tree
├── cmd
│   └── main.go
├── go.mod
└── pkg
    └── test1
        └── test_package.go

通过这样做,你可以运行以下两个命令:

godoc -http=:6060 #http://localhost:6060/pkg/<go.mod中的模块名称>/

GO111MODULE=off godoc -http=:6060 #http://localhost:6060/pkg/testGoDoc/
英文:

You should put your main package into a subdirectory, maybe like this:

~/go/src/testGoDoc$ tree
├── cmd
│   └── main.go
├── go.mod
└── pkg
    └── test1
        └── test_package.go

and by this you can run both commands:

godoc -http=:6060 #http://localhost:6060/pkg/<module name inside go.mod>/

and

GO111MODULE=off godoc -http=:6060 #http://localhost:6060/pkg/testGoDoc/

huangapple
  • 本文由 发表于 2021年12月30日 00:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/70522082.html
匿名

发表评论

匿名网友

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

确定