英文:
What steps are needed to document `package main` in Godoc?
问题
Godoc是一个很好的用于文档化包的工具,但是当用于package main
时似乎不太有用。我只能看到我用//BUG
写给自己的注释和子目录的输出。
Godoc只显示导出的函数,似乎没有办法显示未导出的函数或者main包中的函数。我希望能够看到main包中的函数列表。由于这个功能不被支持,我倾向于在包描述的顶部放置一个函数列表,但这感觉像是一个变通方法。
由于我必须手动更新函数列表,所以我经常将尽可能多的代码放在包中,以便导出并进行文档化。这样做是否明智?我应该如何处理main包中的函数列表?
示例:
命令文档
Package main 实现了一个用于MySQL的Web服务器、模板渲染器和DAL。
<filename.go>
<function>(<signature>)
main.go
main()
bootstrap() error
<更多函数在这里>
BUGS
[filename.go] <任何内容...>
子目录
auth
common
debug
storage
<更多包在这里>
英文:
Godoc is a great tool for documenting packages, however it seems to be less useful when it's used against package main
. I'll see an output that only displays the notes I've written to myself using //BUG
and subdirectories.
Godoc only displays exported functions and seems to have no way to display unexported / functions from main. I would find it useful to see a list of functions in main. Since this isn't supported, I tend to shove a list of functions at the top of the package description but this feels like a workaround.
Since I have to manually update the list of functions, I often put as much code in packages as I can so it's exported and thus documented. Is this a good idea? What should I do about the list of functions in main?
Example:
COMMAND DOCUMENTATION
Package main implements a web server, template renderer and DAL for MySQL.
<filename.go>
<function>(<signature>)
main.go
main()
bootstrap() error
<more functions here>
BUGS
[filename.go] <whatever...>
SUBDIRECTORIES
auth
common
debug
storage
<more packages here>
答案1
得分: 9
你需要构建一个稍微修改过的godoc
版本来文档化主要包。
请参考https://github.com/golang/go/issues/5727。
简而言之:
-
修改
$GOPATH/src/golang.org/x/tools/godoc/server.go
中的以下行:- info.IsMain = pkgname == "main"
- info.IsMain = false && pkgname == "main"
-
使用
go install golang.org/x/tools/cmd/godoc
进行构建和安装。
$GOPATH/bin/godoc
现在应该按照你的要求运行。
英文:
You need to build a slightly modified version of godoc
to document main packages.
Please see https://github.com/golang/go/issues/5727.
tl;dr:
-
Modify the following line in
$GOPATH/src/golang.org/x/tools/godoc/server.go
- info.IsMain = pkgname == "main" + info.IsMain = false && pkgname == "main"
-
Build and install with
go install golang.org/x/tools/cmd/godoc
.
$GOPATH/bin/godoc
should now behave as you wish.
答案2
得分: 8
据我所知,你已经对你的问题有了答案。我可以想到两种替代方案:
- 维护一个显示
main
包函数的godoc分支。(然后你需要在自己的Web服务器上运行一个实例。缺点是直接访问godoc.org获取你的包文档的人将会错过这个功能。) - 将你的
main
包分成子包,使得main
包变得小而简洁。文档可以在这些子包中阅读。但据我所知,这种做法并不普遍。
我认为一般来说,godoc是用于包文档的。main
包的文档实际上只对编辑该包源代码的人有用,因此理论上不需要公开文档。另一方面,这种做法缺乏godoc的良好展示和组织。
作为一种折中方案,如果你真的想公开文档,我建议提供一个关于你的程序架构的概述,而不是逐个函数的详细说明。
英文:
AFAIK, you already have the answer to your question. I can think of two alternative solutions:
- Maintain a fork of godoc that shows functions for
main
packages. (And you'd then have to run an instance of it yourself on a web server. The downside is that people going straight to godoc.org for your package documentation will miss out.) - Separate your
main
packages into sub-packages such that themain
package is small or minimal. Documentation could then be read in those sub-packages. But as far as I know, this is not in widespread practice.
I think in general, godoc is for package documentation. Documentation for main
packages is really only useful to people editing the source code of that package---so the documentation conceivably doesn't need to be publicized. On the other hand, this lacks the nice presentation/organization of godoc.
As a compromise, if you really want to publicize the documentation, I'd recommend an overview of the architecture of your program rather than a play-by-play of each function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论