一文掌握 godoc的使用与规范

简介: 一文掌握 godoc的使用与规范

go doc与godoc


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

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

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

微信截图_20230627113901.png

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

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/,如下:

微信截图_20230627114022.png

添加

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

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

微信截图_20230627114041.png

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


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




godoc个人项目


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

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

微信截图_20230627114125.png

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

微信截图_20230627114137.png

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

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




注释的写法与godoc的解析


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

微信截图_20230627114218.png

gitlab.com/cuishuang/g…

gitlab.com/cuishuang/g…

微信截图_20230627114241.png

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名称)


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

微信截图_20230627114322.png

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

微信截图_20230627114348.png

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

微信截图_20230627114424.png

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

目录
相关文章
|
监控 安全 API
Outlook邮箱API发送邮件的具体步骤
使用Outlook邮箱API发送邮件涉及五个步骤:1) 注册Azure应用并获取API访问权限;2) 构建包含邮件详情的POST请求;3) 发送请求至API端点,附带验证信息和访问令牌;4) 检查API响应状态码确保邮件发送成功;5) 监控邮件发送状态。AokSend提供高效发信服务,支持触发式和SMTP/API接口。确保遵循最佳实践,保证邮件发送安全可靠。
|
5月前
|
人工智能 分布式计算 Serverless
EMR Serverless Daft 如何简化多模态数据处理:视频抽帧、清洗、标注全流程与具身智能实践
阿里云 EMR Serverless Spark 引入 Ray 分布式计算框架与 Daft 高性能数据引擎,为用户提供了一套开箱即用、免运维且极致高效的多模态数据处理基础设施。
|
7月前
|
人工智能 弹性计算 运维
OpenClaw 阿里云实战指南:保姆级部署+高效使用+避坑全解
OpenClaw(社区昵称“小龙虾”)是2026年备受关注的开源AI智能体执行网关,核心定位是让大模型从“对话”升级为“行动”,通过自然语言指令自动完成文件管理、浏览器操控、服务器运维、流程自动化等真实任务,成为个人与团队的24小时数字员工。其具备本地优先、完全开源、永久免费(仅需支付API调用成本)、高适配性等核心优势,完美兼容阿里云各类云服务,成为阿里云用户部署AI代理的首选方案。
593 0
|
11月前
|
人工智能 人机交互 语音技术
输入剧本即可生成漫剧:360 短剧智能体如何落地执行型 AI
2026 年全球将迎来“百亿智能体”时代,这意味着AI从大模型到可以生成能力到可执行能力智能体,360宣布即将发布一款 短剧智能体,即用户只需输入剧本,它就能生成漫剧大片,极大降低影视内容创作门槛。
793 0
|
Go
Golang注释与godoc详解
这篇文章详细介绍了Go语言中注释的格式、位置以及如何使用godoc工具生成和查看项目代码的注释文档。
684 4
Golang注释与godoc详解
|
SQL 安全 关系型数据库
深入理解 MySQL 权限撤销(REVOKE)机制:从语法到安全实践
本文深入解析MySQL中REVOKE语句的语法、权限作用域、用户标识规范及安全实践,涵盖常见错误与最佳策略,助力精准撤销用户权限,提升数据库安全性与合规性。
731 14
|
供应链 安全 Go
Go Modules 详解 -《Go语言实战指南》
Go Modules 是 Go 语言官方推出的依赖管理工具,自 Go 1.11 起引入,Go 1.16 成为默认方式。它解决了第三方依赖版本控制、项目脱离 GOPATH 限制及多模块管理等问题。本文全面讲解了 Go Modules 的基本原理、初始化方法、常用命令(如 `go mod init`、`go get` 等)、依赖管理(添加/升级/删除)、子模块开发以及常见问题排查,帮助开发者高效使用 Go Modules 进行项目管理。
|
移动开发 前端开发 JavaScript
浅谈前端路由原理hash和history
该文章详细解析了前端路由的两种模式——Hash模式与History模式的工作原理及其实现方式,并通过实例代码展示了如何在实际项目中运用这两种路由模式。
|
人工智能 前端开发 Java
DDD四层架构和MVC三层架构的个人理解和学习笔记
领域驱动设计(DDD)是一种以业务为核心的设计方法,与传统MVC架构不同,DDD将业务逻辑拆分为应用层和领域层,更关注业务领域而非数据库设计。其四层架构包括:Interface(接口层)、Application(应用层)、Domain(领域层)和Infrastructure(基础层)。各层职责分明,避免跨层调用,确保业务逻辑清晰。代码实现中,通过DTO、Entity、DO等对象的转换,结合ProtoBuf协议,完成请求与响应的处理流程。为提高复用性,实际项目中可增加Common层存放公共依赖。DDD强调从业务出发设计软件,适应复杂业务场景,是微服务架构的重要设计思想。

热门文章

最新文章