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

5

5

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?

Kultur answered 30/7, 2013 at 10:54 Comment(0)
H
5

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

Howe answered 30/7, 2013 at 11:18 Comment(1)
code.google.com/p/rspace.cmd/doc is gone now, but go doc ioutil.ReadFile seems to have a similar effect.Ankle
A
1

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

Aleppo answered 30/7, 2013 at 12:29 Comment(1)
The vim plugin is gone now. Looks like github.com/fatih/vim-go is where you want to look now maybe.Ankle
M
1

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.

Malinowski answered 5/8, 2013 at 22:42 Comment(0)
D
1

you can run the following in the terminal. (Go version used is 1.22)

go doc -src <packageName>.<methodname>

e.g to view the documentation of ReadAll of io package, just type

go doc -src io.ReadAll

Delirious answered 8/3 at 7:47 Comment(0)
H
0

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.

Halloran answered 30/7, 2013 at 11:19 Comment(1)
I don't see the -ex flag in the godoc documentation (and it doesn't work when I try it). How/where would I use it?Kultur

© 2022 - 2024 — McMap. All rights reserved.