2013-07-30 26 views
2

我正在尋找一種方便的方法來快速查找Go中不同功能和/或軟件包的文檔。我目前的做法是谷歌說ioutil.ReadFile,但這是非常緩慢/不方便。 理想的解決方案可以直接在vim中或者在Go解釋器中使用(建議?)。如何查看特定go功能的文檔?

E.g.在Python中,函數的文檔可以在PyDev中通過懸停在函數上或使用ipython解釋器與例如os.open?help(os.open)

您如何查看Go的特定文檔?

回答

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. 


$ 

  • 使用羅布派克的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

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,它會運行一個Web服務器顯示您(在端口6060默認值)爲所有的軟件包的。

看看godoc documentation。這是一個偉大的工具,它可以做更多。

還有godoc.org,這基本上是一個替代godoc Web前端自動生成,如果具備這樣的http://godoc.org/github.com/golang/groupcache#Context一展身手路徑第三方文檔去主持其他地方的代碼。

+0

我沒有在godoc文檔中看到-ex標誌(當我嘗試它時它不起作用)。我將如何/在哪裏使用它? – rkrzr

1

從Vim內部(假設您已安裝Go's plugin),輸入:Godoc <something>,您將在不離開編輯器的情況下獲得文檔。你可以明顯地將一個鍵映射到這個(沒有參數,如果我沒記錯的話,它會在光標位置搜索該標記)。

也就是說,我經常用Rob Pike's doc來代替,也來自Vim內部(:!doc <something>)。

1

我使用godoc,一個適用於Vim和Sublime的自動完成守護進程。 godoc與GoSublime插件集成在一起。 godoc的好處是它爲所有軟件包提供自動完成功能。除了自動完成之外,我可以在Sublime中按super-., super-H,並提供我輸入的功能的文檔。

我也使用Dash來快速文檔。我使用DashDoc從Sublime快速查看Dash中的某些內容。還有一個dash.vim爲Vim提供了一個Dash插件。

Dash僅提供內置軟件包的文檔,但我主要將其用於其他內容的脫機文檔。到目前爲止,它是調出我發現的HTML格式文檔的最快方式。