前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一文掌握 godoc的使用与规范

一文掌握 godoc的使用与规范

作者头像
fliter
发布2023-06-18 11:13:00
1.2K0
发布2023-06-18 11:13:00
举报
文章被收录于专栏:旅途散记

go doc与godoc

go docgo version,go env类似,都属于go的子命令,用于在命令行输出Go源码相关的文档说明。(仅限于Go源码,无法查看其他项目的源码)

例如可以通过 go doc fmt,查看fmt包相关的文档

也可以更具体到fmt包下面的某个(可导出)func,如doc fmt Printf

可通过`go help doc`查看`go doc`命令的[详细使用方式](https://juejin.cn/post/7100017311637045256):

代码语言:javascript
复制
go help doc
usage: go doc [doc flags] [package|[package.]symbol[.methodOrField]]

Doc prints the documentation comments associated with the item identified by its
arguments (a package, const, func, type, var, method, or struct field)
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.).

Doc accepts zero, one, or two arguments.

Given no arguments, that is, when run as

	go doc

it prints the package documentation for the package in the current directory.
If the package is a command (package main), the exported symbols of the package
are elided from the presentation unless the -cmd flag is provided.

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>[.<methodOrField>]
	go doc [<pkg>.]<sym>[.<methodOrField>]
	go doc [<pkg>.][<sym>.]<methodOrField>

The first item in this list matched by the argument is the one whose documentation
is printed. (See the examples below.) However, if the argument starts with a capital
letter it is assumed to identify a symbol or method in the current directory.

For packages, the order of scanning is determined lexically in breadth-first order.
That is, the package presented is the one that matches the search and is nearest
the root and lexically first at its level of the hierarchy. The GOROOT tree is
always scanned in its entirety before GOPATH.

If there is no package specified or matched, the package in the current
directory is selected, so "go doc Foo" shows the documentation for symbol Foo in
the current package.

The package path must be either a qualified path or a proper suffix of a
path. The go tool's usual package mechanism does not apply: package path
elements like . and ... are not implemented by go doc.

When run with two arguments, the first is a package path (full path or suffix),
and the second is a symbol, or symbol with method or struct field:

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

In all forms, when matching symbols, lower-case letters in the argument match
either case but upper-case letters match exactly. This means that there may be
multiple matches of a lower-case argument in a package if different symbols have
different cases. If this occurs, documentation for all matches is printed.

Examples:
	go doc
		Show documentation for current package.
	go doc Foo
		Show documentation for Foo in the current package.
		(Foo starts with a capital letter so it cannot match
		a package path.)
	go doc encoding/json
		Show documentation for the encoding/json package.
	go doc json
		Shorthand for encoding/json.
	go doc json.Number (or go doc json.number)
		Show documentation and method summary for json.Number.
	go doc json.Number.Int64 (or go doc json.number.int64)
		Show documentation for json.Number's Int64 method.
	go doc cmd/doc
		Show package docs for the doc command.
	go doc -cmd cmd/doc
		Show package docs and exported symbols within the doc command.
	go doc template.new
		Show documentation for html/template's New function.
		(html/template is lexically before text/template)
	go doc text/template.new # One argument
		Show documentation for text/template's New function.
	go doc text/template new # Two arguments
		Show documentation for text/template's New function.

	At least in the current tree, these invocations all print the
	documentation for json.Decoder's Decode method:

	go doc json.Decoder.Decode
	go doc json.decoder.decode
	go doc json.decode
	cd go/src/encoding/json; go doc decode

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.
复制代码

godoc 是一个文档生成工具,通过解析项目.go文件中包含注释的,来生成HTML或文本类型的文档 。通过在本地启动一个web程序,可以在浏览器来展示项目的文档。

Go 秉承 “注释即文档” 的理念,符合 godoc 的文档均从 Go 代码中提取并生成。

Godoc将使用注释的文本来形成包的文档(粒度是某个package,而不是具体到某个.go文件)

官方介绍 Godoc: documenting Go code

Go 1.5之前godoc也是一个内置的命令,新版本的Go不再自带这个命令,需要go get golang.org/x/tools/cmd/godoc来安装,默认是安装到GOBIN 环境变量定义的目录中

godoc -http=127.0.0.1:6060 -play -index 或 简写为 godoc -http=:6060 -play -index

然后再访问 http://localhost:6060/,如下:

添加

-play选项是选择是否开启playgroud,如果有这个flag,会在网页右上方有一个playgroud的入口

-index标记开启搜索索引。这个索引会在服务器启动时创建并维护。如果不加入此标记,那么无论在Web页面还是命令行终端中都是无法进行查询操作的

godoc命令的更多选项可参考官方说明

官方的pkg.go.dev/就是用这种方式生成的,此处不仅能搜索Go的标准库,还能搜索发布上去的其他库


godoc个人项目

godoc默认展示的是官方标准库的内容,个人项目如何使用godoc生成文档?在此试验一下:

新建了一个godoc-demo项目,由于godoc默认解析的是goroot下的src目录,如果我们的项目没有在goroot/src下面,可以新建一个src目录,在该目录下进行git clone操作

src的上一级目录下执行 godoc -http=:9092 -goroot="."

可根据godoc的解析,一一与项目中的注释写法做对照

如何写高大上的 godoc(Go 文档)


注释的写法与godoc的解析

关于注释的写法及在godoc解析后的样式,可参考官方文档 Go Doc Comments以及sync包的文档

gitlab.com/cuishuang/g…

gitlab.com/cuishuang/g…

gitlab.com/cuishuang/g…

gitlab.com/cuishuang/g…

xxx_test.go并不是只有我们常见的单测和benchmark,还可以有其他类型, 可参考 Standard library-testing

特别说明: 针对package的注释

如果一个package下有多个.go文件,不需要每个都写上一样的定义,只需在其中一个文件中写上即可。如 src package下有user.go和order.go,只在user.go文件针对package做了描述

但假如某个package下面,文件已经很多了,很难确定到底应该放到哪个文件中,且后期不好找。 可以添加一个doc.go, 在这个文件里写该 package 的注释。这种方式经常出现在开源项目中


发布到pkg.go.dev

参考 如何"优雅"地发布 go module 模块, 直接搜索gitlab.com/cuishuang/godoc-demo 会找不到结果,这是因为需要通过proxy.golang.org(或goproxy.cn)下载包的时候,module才会自动同步到pkg.go.dev(官方说明)

(https://pkg.go.dev/+module名称)

故而在本地模拟其他用户下一个这个包

之后可以搜到,但会看到Redistributable license的报错,内容显示不全

需要为项目添加开源许可,push之后稍等几分钟,就能看到完整信息了

关于pkg.go.dev更多细节,可参考 万字长文解读 pkg.go.dev 的设计和实现

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-01-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 旅途散记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • go doc与godoc
  • godoc个人项目
  • 注释的写法与godoc的解析
  • 发布到pkg.go.dev
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档