What steps are needed to document `package main` in Godoc?

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

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.

&lt;filename.go&gt;

    &lt;function&gt;(&lt;signature&gt;)

main.go

    main()
    bootstrap() error
    &lt;more functions here&gt;


BUGS

    [filename.go] &lt;whatever...&gt;


SUBDIRECTORIES

    auth
    common
    debug
    storage
    &lt;more packages here&gt;

答案1

得分: 9

你需要构建一个稍微修改过的godoc版本来文档化主要包。

请参考https://github.com/golang/go/issues/5727。

简而言之:

  1. 修改$GOPATH/src/golang.org/x/tools/godoc/server.go中的以下行:

    • info.IsMain = pkgname == "main"
    • info.IsMain = false && pkgname == "main"
  2. 使用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:

  1. Modify the following line in $GOPATH/src/golang.org/x/tools/godoc/server.go

     - info.IsMain = pkgname == &quot;main&quot;
     + info.IsMain = false &amp;&amp; pkgname == &quot;main&quot;
    
  2. Build and install with go install golang.org/x/tools/cmd/godoc.

$GOPATH/bin/godoc should now behave as you wish.

答案2

得分: 8

据我所知,你已经对你的问题有了答案。我可以想到两种替代方案:

  1. 维护一个显示main包函数的godoc分支。(然后你需要在自己的Web服务器上运行一个实例。缺点是直接访问godoc.org获取你的包文档的人将会错过这个功能。)
  2. 将你的main包分成子包,使得main包变得小而简洁。文档可以在这些子包中阅读。但据我所知,这种做法并不普遍。

我认为一般来说,godoc是用于文档的。main包的文档实际上只对编辑该包源代码的人有用,因此理论上不需要公开文档。另一方面,这种做法缺乏godoc的良好展示和组织。

作为一种折中方案,如果你真的想公开文档,我建议提供一个关于你的程序架构的概述,而不是逐个函数的详细说明。

英文:

AFAIK, you already have the answer to your question. I can think of two alternative solutions:

  1. 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.)
  2. Separate your main packages into sub-packages such that the main 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.

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

发表评论

匿名网友

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

确定