0%

go tool doc

定义

go docGo语言的官方工具之一, 用于提取代码注释并生成命令行文档; 有一个相似命令, 可以生成Web页面查看注释, godoc;

使用

1
go help doc 

上面的命令可以输出go doc的帮助文档;
使用方式为: go doc [-u] [-c] [package|[package.]symbol[.methodOrField]]
其允许接受0 , 1, 2个入参

  • 0个入参:

    1
    go doc

    默认输出当前路径下的Package注释; 也就是在.go文件头部的注释内容;

  • 1个入参:

    1
    2
    3
    4
    go doc <pkg>
    go doc <sym>[.<methodOrField>]
    go doc [<pkg>.]<sym>[.<methodOrField>]
    go doc [<pkg>.][<sym>.]<methodOrField>

    其会在GOROOT, GOPATH, currentPath下寻找与语法匹配的标识.

    pkg 标识可以是完整的包路径, 也可以是一个合适的包后缀. 例如go doc json 匹配到的是encoding/json包;

    例如:

    1
    go doc json

    输出json包(encoding/json)下的注释内容;

  • 2个入参:

    1
    go doc <pkg> <sym>[.<methodOrField>]

    例如:

    1
    go doc json Decode

    输出jsonDecode标识的注释;

标识:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Flags:
-all
Show all the documentation for the package.
-c
Respect case when matching symbols.
-cmd
Treat a command (package main) like a regular package.
Otherwise package main's exported symbols are hidden
when showing the package's top-level documentation.
-short
One-line representation for each symbol.
-src
Show the full source code for the symbol. This will
display the full Go source of its declaration and
definition, such as a function definition (including
the body), type declaration or enclosing const
block. The output may therefore include unexported
details.
-u
Show documentation for unexported as well as exported
symbols, methods, and fields.

go doc 依旧提供了可选flag, 支持功能扩展, 上面的定义很明确, 这里不解释;