如何查看特定go函数的文档?

时间:2013-07-30 10:54:18

标签: vim go

我正在寻找一种方便的方法来快速查找Go中不同功能和/或包的文档。我目前的方法是谷歌说ioutil.ReadFile,但这很慢/不方便。 理想的解决方案可以直接在vim中使用,也可以在Go解释器中使用(建议?)。

E.g。在Python中,函数的文档可以通过将鼠标悬停在函数上或使用ipython解释器来显示在PyDev中。 os.open?help(os.open)

如何查看Go的具体文档?

4 个答案:

答案 0 :(得分:4)

你有很多可能性:

  • 浏览http://golang.org/pkg/和/或使用“搜索”框,它甚至可以知道正则表达式!
  • 运行本地godoc并为所有本地安装的软件包获取相同内容。更快更离线!
  • 从命令行查询godoc:

$ godoc io/ioutil ReadFile
PACKAGE DOCUMENTATION

package ioutil
    import "io/ioutil"



FUNCTIONS

func ReadFile(filename string) ([]byte, error)
    ReadFile reads the file named by filename and returns the contents. A
    successful call returns err == nil, not err == EOF. Because ReadFile
    reads the whole file, it does not treat an EOF from Read as an error to
    be reported.


$ 

  • 使用Rob Pike的doc [0]。

$ doc ioutil.ReadFile
http://golang.org/pkg/io/ioutil/#ReadFile
/home/jnml/go/src/pkg/io/ioutil/ioutil.go:48:
// ReadFile reads the file named by filename and returns the contents.
// A successful call returns err == nil, not err == EOF. Because ReadFile
// reads the whole file, it does not treat an EOF from Read as an error
// to be reported.
func ReadFile(filename string) ([]byte, error)

$ 

[0]:$ go get code.google.com/p/rspace.cmd/doc

答案 1 :(得分:1)

从Vim内部(假设您已安装Go's plugin),键入:Godoc <something>,您将在不离开编辑器的情况下获得文档。你可以明显地将一个键映射到这个(没有参数,如果我没记错的话,它会在光标位置搜索令牌)。

话虽如此,我经常使用Rob Pike's doc来代替Vim(:!doc <something>)。

答案 2 :(得分:1)

我使用godoc,一个适用于Vim和Sublime的自动完成守护程序。 godoc与GoSublime插件集成在一起。关于godoc的好处是它为所有包提供自动完成功能。除了自动完成,我可以在Sublime中按super-., super-H,它会提供我输入的函数的文档。

我还使用Dash来快速记录文档。我使用DashDoc从Sublime快速查找Dash中的内容。还有dash.vim从Vim为Dash提供插件。

Dash仅提供内置包的文档,但我主要将其用于其他内容的脱机文档。到目前为止,这是提出我找到的HTML格式文档的最快方法。

答案 3 :(得分:0)

您可以使用godoc命令行工具。 godoc os Open将查找os.Open并仅显示与该功能相关的内容。您可以使用-ex打开示例。

示例:

$ godoc os Signal

PACKAGE DOCUMENTATION

package os
    import "os"



TYPES

type File struct {
    // contains filtered or unexported fields
}
    File represents an open file descriptor.


func Open(name string) (file *File, err error)
    Open opens the named file for reading. If successful, methods on the
    returned file can be used for reading; the associated file descriptor
    has mode O_RDONLY. If there is an error, it will be of type *PathError.

您还可以运行godoc -http,它将运行一个网络服务器,向您显示所有软件包的文档(默认位于端口6060)。

看看godoc documentation。这是一个很棒的工具,它可以做得更多。

还有godoc.org,它基本上是另一个godoc网络前端,如果提供了像http://godoc.org/github.com/golang/groupcache#Context这样的go路径,它会自动为其他地方托管的第三方go代码生成文档。