一文掌握 godoc的使用与规范

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

go doc与godoc


go docgo 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 的设计和实现

目录
相关文章
|
JavaScript 搜索推荐 程序员
Vuepress + gitee五分钟免费搭建个人博客(保姆级教程)
前言 作为一个程序员,没有折腾过个人博客是不算完整的。技术文章的输出是我们程序员能力的一种体现,也是一种非常好的个人总结。 市面上有很多搭建个人博客的工具或框架,包括hexo、wordpress等等。不可否认,市面上有些博客系统做得很好,博客主题也很丰富,但是往往存在一个问题:比较重。 作为一个Vue程序员,我就比较推荐使用vuepress搭建个人博客,因为它毕竟是Vue出品的,大家熟知的vue官方就是利用vuepress搭建的。 本篇文章就从零开始教大家搭建一个免费的博客,零基础小白也可以学习哦!
4225 0
Vuepress + gitee五分钟免费搭建个人博客(保姆级教程)
|
7月前
解决Minikube运行拉取镜像慢的问题
使用国内镜像源加速Minikube启动:先拉取阿里云托管的kicbase基础镜像,删除原有集群,再通过指定镜像和中国区镜像源启动Minikube,提升部署效率。
1239 0
|
Go
Golang注释与godoc详解
这篇文章详细介绍了Go语言中注释的格式、位置以及如何使用godoc工具生成和查看项目代码的注释文档。
541 4
Golang注释与godoc详解
|
11月前
|
供应链 安全 Go
Go Modules 详解 -《Go语言实战指南》
Go Modules 是 Go 语言官方推出的依赖管理工具,自 Go 1.11 起引入,Go 1.16 成为默认方式。它解决了第三方依赖版本控制、项目脱离 GOPATH 限制及多模块管理等问题。本文全面讲解了 Go Modules 的基本原理、初始化方法、常用命令(如 `go mod init`、`go get` 等)、依赖管理(添加/升级/删除)、子模块开发以及常见问题排查,帮助开发者高效使用 Go Modules 进行项目管理。
|
编译器 Go API
go generate指南:代码自动生成
go generate指南:代码自动生成
4538 0
|
自然语言处理 运维 开发工具
深入探讨了 NeoVim 相较于传统 Vim 的优势,包括更好的扩展性、现代化的界面和用户体验、多语言编程支持、强大的异步处理能力、更好的协作支持、持续的更新和改进、活跃的社区以及与现代开发工具的集成
本文深入探讨了 NeoVim 相较于传统 Vim 的优势,包括更好的扩展性、现代化的界面和用户体验、多语言编程支持、强大的异步处理能力、更好的协作支持、持续的更新和改进、活跃的社区以及与现代开发工具的集成。通过命令对比,展示了两者在启动、配置、模式切换、移动编辑、搜索替换、插件管理、文件操作、窗口缓冲区管理和高级功能等方面的差异。总结部分强调了 NeoVim 在多个方面的显著优势,解释了为什么越来越多的运维人员选择 NeoVim。
1570 3
|
SQL 关系型数据库 Go
Golang ORM框架介绍及比较
Golang ORM框架介绍及比较
|
移动开发 前端开发 JavaScript
浅谈前端路由原理hash和history
该文章详细解析了前端路由的两种模式——Hash模式与History模式的工作原理及其实现方式,并通过实例代码展示了如何在实际项目中运用这两种路由模式。
|
运维 数据安全/隐私保护 Docker
Docker自建仓库之Docker Registry部署实战
关于如何使用Docker Registry镜像搭建本地私有Docker仓库的实战教程,包括了下载镜像、创建授权目录和用户名密码、启动Registry容器、验证端口和容器、测试登录仓库、上传和下载镜像的详细步骤。
4885 5
|
JSON Go API
一文搞懂 Golang 高性能日志库 - Zap
一文搞懂 Golang 高性能日志库 - Zap
2410 2