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?
You have many possibilities:
$ 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
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 "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.
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.
From within Vim (assuming you've installed Go's plugin), type :Godoc <something>
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 <something>
).
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.