godoc 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

godoc 命令安装:


-bash/zsh: godoc command not found

# Windows (WSL2)
sudo apt-get update sudo apt-get install golang-golang-x-tools

# Debian
apt-get install golang-golang-x-tools

# Ubuntu
apt-get install golang-golang-x-tools

# Arch Linux
pacman -S golang-godoc-1

# Kali Linux
apt-get install golang-golang-x-tools

# Fedora
dnf install golang-godoc-1

# Raspbian
apt-get install golang-golang-x-tools

# Dockerfile
dockerfile.run/godoc

godoc 命令补充说明:


godoc 提取并生成 Go 程序的文档。

它有两种模式:

在没有 -http 标志下,godoc 将以命令行模式(command-line mode)运行,并将纯文本文档打印到标准输出并退出。如果库包和同名命令都存在,则使用前缀 cmd/ 将强制将文档放在命令上,而不是库包上。如果指定了 -src 标志,godoc 将以 Go 源代码形式打印包的导出接口,或特定导出语言实体的实现:

godoc fmt                # documentation for package fmt
godoc fmt Printf         # documentation for fmt.Printf
godoc cmd/go             # force documentation for the go command
godoc -src fmt           # fmt package interface in Go source form
godoc -src fmt Printf    # implementation of fmt.Printf

在命令行模式下,-q 标志可针对作为 Web 服务器运行的 godoc 启用搜索查询。如果未使用 -server 标志指定明确的服务器地址,godoc 将首先尝试 localhost:6060,然后尝试 http://golang.org。

godoc -q Reader
godoc -q math.Sin
godoc -server=:6060 -q sin

使用 -http 标志,它作为 Web 服务器运行并将文档显示为网页。

godoc -http=:6060

默认情况下,godoc 通过 $GOROOT$GOPATH(如已设置)查找找到的包。可以通过 -path 标志指定其他目录,该标志接受以冒号分隔的路径列表;无根路径相对于当前工作目录。每个路径都按出现顺序被视为包的附加根。最后一个(绝对)路径元素是包路径的前缀。例如,给定标志值:

path=".:/home/bar:/public"

对于在 /home/user/godoc 中启动的 godoc,绝对路径映射到包路径如下:

/home/user/godoc/x -> godoc/x
/home/bar/x        -> bar/x
/public/x          -> public/x

godoc 命令语法:


godoc [flag] command [ name ... ]

godoc 命令选项:


-v
verbose mode

-q
arguments are considered search queries: a legal query is a single identifier (such as ToLower) or a qualified identifier (such as math.Sin).

-src
print (exported) source in command-line mode

-tabwidth=4
width of tabs in units of spaces

-timestamps=true
show timestamps with directory listings

-index
enable identifier and full text search index (no search box is shown if -index is not set)

-index_files=
glob pattern specifying index files; if not empty, the index is read from these files in sorted order

-index_throttle=0.75
index throttle value; a value of 0 means no time is allocated to the indexer (the indexer will never finish), a value of 1.0 means that index creation is running at full throttle (other goroutines may get no time while the index is built)

-write_index=false
write index to a file; the file name must be specified with -index_files

-maxresults=10000
maximum number of full text search results shown (no full text index is built if maxresults <= 0)

-path=
additional package directories (colon-separated)

-html
print HTML in command-line mode

-goroot=$GOROOT
Go root directory

-http=addr
HTTP service address (e.g., '127.0.0.1:6060' or just ':6060')

-server=addr
webserver address for command line searches

-templates=
directory containing alternate template files; if set, the directory may provide alternative template files for the files in $GOROOT/lib/godoc

-url=path
print to standard output the data that would be served by an HTTP request for path

-zip=
zip file providing the file system to serve; disabled if empty

godoc 命令实例:


godoc 显示包 fmt 的帮助:

godoc fmt

godoc 显示 fmt 包中的 Printf 函数的帮助:

godoc fmt Printf

godoc 在端口 6060 上将文档作为 Web 服务器提供:

godoc -http=:6060

godoc 创建索引文件:

godoc -write_index -index_files=path/to/file

godoc 使用给定的索引文件搜索文档:

godoc -http=:6060 -index -index_files=path/to/file

godoc 命令扩展阅读:




godoc 命令评论