wingedrhino
wingedrhino

Reputation: 162

How can I jump to the documentation of the type of a field in a struct or a method I'm browsing in godoc?

I'm still struggling to word the question in a way that is quick to understand, so I'll instead document the steps I followed to encounter the issue I am facing.

  1. I am looking up documentation for the Request struct in net/http package. I do this via godoc net/http Request
  2. Now browsing through the documentation, the first field Request struct has is URL which is of type *url.URL whose documentation I'd like to lookup.
  3. But godoc shares no details as to which import path it is a part of. I only know that the package name itself is "url" but its canonical import path would be something like "some/path/url" which I would like to know so that I can look it up and figure out the URL field.

What steps do I follow to get to the specific url package being referenced in the above piece of documentation ?

Upvotes: 0

Views: 605

Answers (2)

Dave C
Dave C

Reputation: 7888

Go 1.5

In Go 1.5 you can use the new go doc sub-command:

Show documentation for package or symbol

Usage:

go doc [-u] [-c] [package|[package.]symbol[.method]]

Doc prints the documentation comments associated with the item identified by its arguments (a package, const, func, type, var, or method) followed by a one-line summary of each of the first-level items "under" that item (package-level declarations for a package, methods for a type, etc.).

[…]

When run with one argument, the argument is treated as a Go-syntax-like representation of the item to be documented. What the argument selects depends on what is installed in GOROOT and GOPATH, as well as the form of the argument, which is schematically one of these:

go doc <pkg>
go doc <sym>[.<method>]
go doc [<pkg>].<sym>[.<method>]

The first item in this list matched by the argument is the one whose documentation is printed. (See the examples below.) For packages, the order of scanning is determined lexically, but the GOROOT tree is always scanned before GOPATH.

So for your example just go doc url.URL without you needing to know that the url package is imported as net/url.

Pre Go 1.5

Until Go 1.5 is released there is a very simalar command that you can install with:

go install robpike.io/cmd/doc

Once installed you'd do doc url.URL.

Upvotes: 2

Alex Netkachov
Alex Netkachov

Reputation: 13542

You can run queries with godoc:

$ godoc -q URL | less
QUERY
        URL

DID YOU MEAN

        url

Types
    net/url.URL
    html/template.URL
...
...
...

The net/url in Types is a good candidate to look at.

$ godoc net/url | less
PACKAGE DOCUMENTATION

package url
    import "net/url"

    Package url parses URLs and implements query escaping. See RFC 3986.

FUNCTIONS
...
...
...

Upvotes: 1

Related Questions