Search code examples
vimgo

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


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?


Solution

  • 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 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