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

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

How can you see the documentation for a specific go function?

问题

我正在寻找一种方便快捷的方式来查找Go中不同函数和/或包的文档。我目前的方法是在谷歌上搜索ioutil.ReadFile,但这样做很慢/不方便。
理想的解决方案可以直接在vim中工作,或者可能在Go解释器中(有什么建议吗?)。

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

你如何查看Go的特定文档?

英文:

I'm looking for a convenient way to quickly look up the documentation for different functions and/or packages in Go. My current approach is to google for say ioutil.ReadFile, but that is pretty slow/inconvenient.
The ideal solution would work directly in vim or maybe in a Go interpreter(suggestions?).

E.g. in Python the documentation of a function can be shown in PyDev by hovering over a function or using the ipython interpreter with e.g. os.open? or help(os.open).

How do you view specific documentation for Go?

答案1

得分: 5

你有很多选择:

  • 浏览 http://golang.org/pkg/ 并/或使用“搜索”框,它甚至支持正则表达式!
  • 运行本地的 godoc 并获取所有本地安装的包的相同信息。更快且离线!
  • 从命令行查询 godoc:

$ godoc io/ioutil ReadFile
包文档

package ioutil
    import "io/ioutil"



函数

func ReadFile(filename string) ([]byte, error)
    ReadFile 读取由 filename 指定的文件并返回其内容。成功的调用返回 err == nil,而不是 err == EOF。因为 ReadFile 读取整个文件,所以它不会将来自 Read 的 EOF 视为要报告的错误。


$ 

  • 使用 Rob Pike 的 doc0

$ doc ioutil.ReadFile
http://golang.org/pkg/io/ioutil/#ReadFile
/home/jnml/go/src/pkg/io/ioutil/ioutil.go:48:
// ReadFile 读取由 filename 指定的文件并返回其内容。
// 成功的调用返回 err == nil,而不是 err == EOF。因为 ReadFile 读取整个文件,
// 所以它不会将来自 Read 的 EOF 视为要报告的错误。
func ReadFile(filename string) ([]byte, error)

$ 

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

英文:

You have many possibilities:

  • Browse http://golang.org/pkg/ and/or use the "Search" box, which even knows regexps!
  • Run local godoc and get the same for all locally installed packages. Faster and off-line!
  • Query godoc from the command line:

$ 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.


$ 

  • Use Rob Pike's doc0.

$ 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

答案2

得分: 1

从Vim内部(假设您已经安装了Go的插件),键入:Godoc <something>,您将在不离开编辑器的情况下获得文档。您可以显然地将一个键映射到这个命令(如果我记得正确的话,不带参数的话,它会搜索光标位置处的标记)。

话虽如此,我经常在Vim中使用Rob Pike的doc(也是:!doc <something>)。

英文:

From within Vim (assuming you've installed Go's plugin), type :Godoc &lt;something&gt; and you will get the documentation without leaving the editor. You can obviously map a key to this (without argument, if I recall correctly, it searches for the token at the cursor location).

That being said, I often use Rob Pike's doc instead, from within Vim too (:!doc &lt;something&gt;).

答案3

得分: 1

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

我还使用Dash进行快速文档查询。我使用DashDoc从Sublime中快速查找Dash中的内容。还有dash.vim提供了一个从Vim到Dash的插件。

Dash只提供内置包的文档,但我主要用它来获取其他内容的离线文档。到目前为止,这是我找到的最快的查看HTML格式文档的方法。

英文:

I use godoc, an auto-complete daemon which works for Vim and Sublime. godoc is integrated with the GoSublime plugin. The nice thing about godoc is it provides auto-complete for all packages. Besides auto-complete, I can press super-., super-H in Sublime and it gives documentation of the function I've typed in.

I also use Dash for quick documentation. I use DashDoc to quickly look something up in Dash from Sublime. There is also dash.vim that provides a plugin to Dash from Vim.

Dash only provides documentation for built-in packages, but I mostly use it for offline documentation for other things. So far it's the quickest way to bring up the HTML formatted documentation that I've found.

答案4

得分: 0

你可以使用godoc命令行工具来实现。godoc os Open将查找os.Open并仅显示与该函数相关的内容。你可以使用-ex选项来打开示例。

示例:

$ godoc os Signal

包文档

package os
    import "os"



类型

type File struct {
    // 包含已过滤或未导出的字段
}
    File表示一个打开的文件描述符。


func Open(name string) (file *File, err error)
    Open打开指定名称的文件进行读取。如果成功,返回的文件可以用于读取;关联的文件描述符具有O_RDONLY模式。如果出现错误,它将是*PathError类型的。

你还可以运行godoc -http,它将运行一个Web服务器,显示所有包的文档(默认端口为6060)。

请查看godoc文档。它是一个很棒的工具,可以做更多的事情。

还有godoc.org,它基本上是一个替代的godoc Web前端,如果提供了类似这样的go路径,它会自动生成第三方go代码的文档,如http://godoc.org/github.com/golang/groupcache#Context。

英文:

You can use the godoc command line tool for that. godoc os Open will lookup os.Open and show only what's relevant to that function. You can turn on examples with -ex.

Example:

$ godoc os Signal

PACKAGE DOCUMENTATION

package os
    import &quot;os&quot;



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.

You can also run godoc -http and it will run a webserver showing you the documentation for all your packages (default at port 6060).

Take a look at the godoc documentation. It's a great tool and it can do much more.

There is also godoc.org, which is basically an alternative godoc web frontend that automatically generates docs for 3rd party go code hosted somewhere else if provided with a go path like this http://godoc.org/github.com/golang/groupcache#Context.

huangapple
  • 本文由 发表于 2013年7月30日 18:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/17945068.html
匿名

发表评论

匿名网友

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

确定