你可以使用godoc将Go程序的简单文档作为网页提供。

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

How do you serve simple documentation for go programs using godoc as a webpage?

问题

我试图将一个特定的本地go文件作为文档网页进行服务,但是无法实现。

官方的godoc文档中提到:

> 使用 -http 标志(即 godoc 命令),它将作为一个 Web 服务器运行,并将文档呈现为一个网页。

user_me$ godoc -http=:6060

这样确实创建了一个类似于go页面的东西,但它没有渲染我想要渲染的特定文件。所以我尝试提供我想要的文件名:

user_me$ godoc -http=:6000 hello.go

然而,它只是回复:

usage: godoc package [name ...]
	godoc -http=:6060
  -ex=false: 在命令行模式下显示示例
  -goroot="/usr/local/go": Go 根目录
  -html=false: 在命令行模式下打印 HTML
  -http="": HTTP 服务地址(例如,':6060')
  -httptest.serve="": 如果非空,httptest.NewServer 将在此地址上提供服务并阻塞
  -index=false: 启用搜索索引
  -index_files="": 指定索引文件的 glob 模式;如果非空,则按排序顺序从这些文件中读取索引
  -index_throttle=0.75: 索引节流值;0.0 = 不分配时间,1.0 = 全速运行
  -links=true: 将标识符链接到其声明
  -maxresults=10000: 显示的全文搜索结果的最大数量
  -notes="BUG": 正则表达式匹配要显示的注释标记
  -play=false: 在 Web 界面中启用 playground
  -q=false: 参数被视为搜索查询
  -server="": 用于命令行搜索的 Web 服务器地址
  -src=false: 在命令行模式下打印(导出的)源代码
  -tabwidth=4: 制表符宽度
  -templates="": 包含替代模板文件的目录
  -timestamps=false: 在目录列表中显示时间戳
  -url="": 打印指定 URL 的 HTML
  -v=false: 详细模式
  -write_index=false: 将索引写入文件;必须使用 -index_files 指定文件名
  -zip="": 提供要提供的文件系统的 zip 文件;如果为空,则禁用

我还尝试了:

user_me$ godoc -url="localhost:8080" hello.go

但它没有起作用。

我还尝试了:

godoc -server=localhost:8080 hello.go

但它回复:

2014/07/01 10:45:56 打开 /usr/local/go/src/pkg/hello.go:没有该文件或目录

我甚至尝试只生成html文件本身:

godoc -html hello.go > hello.html

与上面的错误相同。

我还尝试了(因为它抱怨在 pkg 目录中没有文件):

godoc -html -goroo=$GOPATH hello.go > hello.html

最后,我放弃了。我不知道这个godoc是如何工作的。我安装了 hello.go 程序,以便在工作区的 pkg 文件中有一些内容。你如何为你的代码生成一个带有文档的网页?

英文:

I was trying to serve a specific local go file as a documentation web page, but was not able to do it.

The official godoc documentation says:

> With the -http flag (i.e. the godoc command), it runs as a web server and presents the documentation as a web page.

user_me$ godoc -http=:6060

This does create something similar as the go page but it does not render the specific file that I want to render. So I tried to provide the name of the file I wanted:

user_me$ godoc -http=:6000 hello.go

However, it just replies with:

usage: godoc package [name ...]
	godoc -http=:6060
  -ex=false: show examples in command line mode
  -goroot="/usr/local/go": Go root directory
  -html=false: print HTML in command-line mode
  -http="": HTTP service address (e.g., ':6060')
  -httptest.serve="": if non-empty, httptest.NewServer serves on this address and blocks
  -index=false: enable search index
  -index_files="": glob pattern specifying index files;if not empty, the index is read from these files in sorted order
  -index_throttle=0.75: index throttle value; 0.0 = no time allocated, 1.0 = full throttle
  -links=true: link identifiers to their declarations
  -maxresults=10000: maximum number of full text search results shown
  -notes="BUG": regular expression matching note markers to show
  -play=false: enable playground in web interface
  -q=false: arguments are considered search queries
  -server="": webserver address for command line searches
  -src=false: print (exported) source in command-line mode
  -tabwidth=4: tab width
  -templates="": directory containing alternate template files
  -timestamps=false: show timestamps with directory listings
  -url="": print HTML for named URL
  -v=false: verbose mode
  -write_index=false: write index to a file; the file name must be specified with -index_files
  -zip="": zip file providing the file system to serve; disabled if empty

I also tried:

user_me$ godoc -url="localhost:8080" hello.go

but it didn't work.

I also tried:

godoc -server=localhost:8080 hello.go

but it replied with:

2014/07/01 10:45:56 open /usr/local/go/src/pkg/hello.go: no such file or directory

I even tried just generating the html thing itself:

godoc -html hello.go > hello.html

same error as above.

I also tried (since it was complaining that there was no file in the pkg dir):

godoc -html -goroo=$GOPATH hello.go > hello.html

At the end, I gave up. I don't know how this godoc thing works. I installed the hello.go program so that I there was something in the pkg file in the workspace. How do you generate a webpage with your documentation for your code?

答案1

得分: 24

godoc操作的是包和类型名称,而不是文件名。

例如,要了解io/ioutil包:

  • 文本输出:godoc io/ioutil

  • 只查看ReadAll函数:godoc io/ioutil ReadAll

  • 以HTML格式查看:godoc -html io/ioutil ReadAll

  • 在浏览器中:

    • godoc -http=:6060
    • 点击"Packages",然后从那里导航
    • 或直接访问http://localhost:6060/pkg/io/ioutil#ReadAll

要查看自己代码的文档,需要将其包含在GOPATH中。

假设你的GOPATH包括$HOME/go/src,你感兴趣的文件是$HOME/go/src/hey/world/doc.go,你可以运行:

godoc hey/world

...或者以HTTP模式启动godoc,并浏览到http://localhost:6060/pkg/hey/world

英文:

godoc operates on package and type names, not filenames.

For example, to learn about io/ioutil package:

  • text output: godoc io/ioutil

  • just the ReadAll function: godoc io/ioutil ReadAll

  • in HTML: godoc -html io/ioutil ReadAll

  • in the browser:

    • godoc -http=:6060
    • click Packages and navigate from there
    • or go directly to http://localhost:6060/pkg/io/ioutil#ReadAll

To view documentation for your own code, it has to be included in your GOPATH.

Suppose your GOPATH includes $HOME/go/src, and the file you are interested in is $HOME/go/src/hey/world/doc.go, you would run:

godoc hey/world

...or start godoc in HTTP mode and browse to http://localhost:6060/pkg/hey/world

答案2

得分: 9

默认情况下,godoc 会查找通过 $GOROOT 和 $GOPATH 找到的包。所以,假设你的包在 Go 工作区中,即在 GOPATH 中,你可以运行以下命令:

godoc fmt

这将打印出 fmt 包的文档。

如果你想为位于 $GOPATH/src/github.com/abcd/foo 位置的包 foo 生成文档,你应该运行以下命令:

godoc github.com/abcd/foo

使用 -http 标志,godoc 作为一个 Web 服务器运行,并将文档呈现为一个网页。

godoc -http=:6060

现在在浏览器中导航到 http://localhost:6060/pkg/github.com/abcd/foo,以查找以网页形式呈现的文档。

可以使用 -play 标志在 Web 界面中启用 playground。

英文:

By default, godoc looks at the packages it finds via $GOROOT and $GOPATH. So given that your package is in Go workspace i.e in GOPATH, you can run

godoc fmt

which prints out documentation for fmt package.

If you want to generate docs for your package foo which is in $GOPATH/src/github.com/abcd/foo location, you should run

godoc github.com/abcd/foo

With the -http flag, godoc runs as a web server and presents the documentation as a web page.

godoc -http=:6060

Now navigate to http://localhost:6060/pkg/github.com/abcd/foo in browser to find docs as web page.

The -play flag can be used to enable playground in web interface.

答案3

得分: 2

展示为您自己的代码生成的HTML文档的步骤如下:

第一步:在命令行中启动文档Web服务器,即:

C:\>godoc -http=:6060

第二步:打开浏览器,并使用显式URL指向您的代码所在的文件夹。

URL的结构来自于您的GOPATH下的文件夹名称。

例如:

如果我的GOPATH是c:\go,并且我的代码在c:\go\src\myfolder\mysubfolder中,

我将使用的URL是http://localhost:6060/pkg/myfolder/mysubfolder,这将显示该文件夹中.go文件的HTML页面。

您还可以使用URL http://localhost:6060/pkg/myfolder,它将有一个指向mysubfolder的链接。

注意:

  • 我不确定如何在http://localhost:6060/pkg级别上查看您的本地代码,也许您不能。
  • 可以“指定其他路径”,所以我认为它不一定是src文件夹,请参阅https://blog.golang.org/godoc-documenting-go-code。
英文:

To show HTML doc generated for your own code

Step 1) At command line start up the document web server, that is:

C:\>godoc -http=:6060

Step 2) Open a browser and use an explicit url the folder your code is.

The URL structure comes from the folder names under your GOPATH.

For example:

If my GOPATH is c:\go and I have code in c:\go\src\myfolder\mysubfolder

The URL I would uses is http://localhost:6060/pkg/myfolder/mysubfolder and this would show an HTML page for the .go files in there

Also you can use URL http://localhost:6060/pkg/myfolder, which will have a link to mysubfolder

Notes:

  • I'm not sure how to see your local code at the the http://localhost:6060/pkg level, maybe you can't
  • It is possible to "specify additional paths" so I don't think it has to be the src folder, see https://blog.golang.org/godoc-documenting-go-code

答案4

得分: 0

运行godoc命令可以工作,但是速度非常慢,因为它会为标准库中的每个包生成文档,而我只关心我正在使用的本地包。为此,如果你的包在一个名为something的文件夹中,你可以将该文件夹移动到如下位置:

godoc/src/something

然后,进入godoc文件夹,运行以下命令:

godoc -goroot .

然后,浏览到localhost:6060

英文:

Running godoc on its own worked for me, but was really slow because it
generates docs for every single package in the standard library, while I only
care about the local package that I am working on. To that end, if your package is in a folder called something, you can move
the folder so that it looks like this:

godoc/src/something

Then, go to the godoc folder, and run

godoc -goroot .

Then, browse to localhost:6060.

答案5

得分: 0

在Linux上,假设你已经进入了你想要阅读文档的包的目录。

如果你正在使用Go模块,你可以运行以下命令:

godoc -http=:6060 & xdg-open http://localhost:6060/pkg/$(go list -m)

它使用-m标志来获取包的路径,即使根模块目录中没有任何.go文件。

如果你还没有使用模块,你可以运行以下命令:

godoc -http=:6060 & xdg-open http://localhost:6060/pkg/$(go list -f "{{.ImportPath}}")

请注意,与-m不同,如果目录中没有.go文件,这个命令将无法正常工作。

在https://golang.org/pkg/cmd/go/internal/list/上查看go list子命令的帮助信息。

英文:

On linux, and assuming you have cd'd into the package of which you want to read the documentation.

if you are using go modules, you can run below command

godoc -http=:6060 & xdg-open http://localhost:6060/pkg/$(go list -m)

It uses the -m flag to get the package path even though the root module directory does not contain any .go file.

If you are not yet using modules, you can run,

godoc -http=:6060 & xdg-open http://localhost:6060/pkg/$(go list -f "{{.ImportPath}}")

Note that unlike -m this command will not work appropriately if there is no .go files into the directory.

Check the go list subcommand help at https://golang.org/pkg/cmd/go/internal/list/

huangapple
  • 本文由 发表于 2014年7月2日 00:09:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/24514885.html
匿名

发表评论

匿名网友

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

确定