你可以通过在godoc中浏览的结构体或方法上跳转到字段类型的文档。

huangapple go评论89阅读模式
英文:

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

问题

我仍然在努力用简洁易懂的方式表达问题,所以我将记录我遇到的问题的步骤。

  1. 我正在查找 net/http 包中 Request 结构的文档。我通过 godoc net/http Request 进行查找。
  2. 现在浏览文档时,Request 结构的第一个字段是 URL,其类型为 *url.URL,我想查找它的文档。
  3. 但是 godoc 没有提供关于它所属的导入路径的详细信息。我只知道包名本身是 "url",但它的规范导入路径可能类似于 "some/path/url",我想知道它的导入路径,以便我可以查找并找出 URL 字段。

我应该按照什么步骤才能找到上述文档中引用的特定的 url 包?

英文:

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 ?

答案1

得分: 2

Go 1.5

在Go 1.5中,你可以使用新的go doc子命令:

显示包或符号的文档

用法:

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

Doc打印与其参数所标识的项(包、常量、函数、类型、变量或方法)相关联的文档注释,然后是每个第一级项的一行摘要,这些项位于该项的“下方”(对于包的包级声明,对于类型的方法等)。

[…]

当使用一个参数运行时,该参数被视为要文档化项的类似Go语法的表示。参数选择的内容取决于GOROOT和GOPATH中安装的内容,以及参数的形式,大致如下:

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

与参数匹配的此列表中的第一项是打印其文档的项。(请参见下面的示例。)对于包,扫描的顺序是按词法确定的,但始终在GOPATH之前扫描GOROOT树。

所以对于你的示例,只需执行go doc url.URL,而不需要知道url包是作为net/url导入的。

Go 1.5之前

在Go 1.5发布之前,你可以使用一个非常相似的命令进行安装:

go install robpike.io/cmd/doc

安装完成后,你可以执行doc url.URL

英文:

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.

答案2

得分: 1

你可以使用godoc来运行查询:

$ godoc -q URL | less
QUERY
        URL

DID YOU MEAN

        url

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

在Types中的net/url是一个值得查看的候选项。

$ godoc net/url | less
PACKAGE DOCUMENTATION

package url
    import "net/url"

    Package url解析URL并实现了查询转义。参见RFC 3986。

FUNCTIONS
...
...
...

英文:

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 &quot;net/url&quot;

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

FUNCTIONS
...
...
...

huangapple
  • 本文由 发表于 2015年7月27日 16:33:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/31648354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定