一文掌握 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 的设计和实现

目录
相关文章
|
中间件 Go 数据库
Go开发者必读:Gin框架的实战技巧与最佳实践
在当今快速发展的互联网时代,Web开发的需求日益增长。Go语言以其简洁、高效、并发性强的特点,成为了开发者们的首选。而在Go语言的众多Web框架中,Gin无疑是其中的佼佼者。本文将深入探讨Gin框架的特性、优势以及如何利用Gin构建高性能的Web应用。
|
编译器 Go API
go generate指南:代码自动生成
go generate指南:代码自动生成
3526 0
|
Go
Golang注释与godoc详解
这篇文章详细介绍了Go语言中注释的格式、位置以及如何使用godoc工具生成和查看项目代码的注释文档。
317 4
Golang注释与godoc详解
|
10月前
|
JSON 安全 Go
Go语言中使用JWT鉴权、Token刷新完整示例,拿去直接用!
本文介绍了如何在 Go 语言中使用 Gin 框架实现 JWT 用户认证和安全保护。JWT(JSON Web Token)是一种轻量、高效的认证与授权解决方案,特别适合微服务架构。文章详细讲解了 JWT 的基本概念、结构以及如何在 Gin 中生成、解析和刷新 JWT。通过示例代码,展示了如何在实际项目中应用 JWT,确保用户身份验证和数据安全。完整代码可在 GitHub 仓库中查看。
1689 1
|
人工智能 网络协议 应用服务中间件
Golang 搭建 WebSocket 应用(一) - 初识 gorilla/websocket
Golang 搭建 WebSocket 应用(一) - 初识 gorilla/websocket
1240 1
|
12月前
|
存储 JSON Go
在Gin框架中优雅地处理HTTP请求体中的JSON数据
在Gin框架中优雅地处理HTTP请求体中的JSON数据
|
10月前
|
存储 Go PHP
Go语言中的加解密利器:go-crypto库全解析
在软件开发中,数据安全和隐私保护至关重要。`go-crypto` 是一个专为 Golang 设计的加密解密工具库,支持 AES 和 RSA 等加密算法,帮助开发者轻松实现数据的加密和解密,保障数据传输和存储的安全性。本文将详细介绍 `go-crypto` 的安装、特性及应用实例。
503 0
|
运维 Kubernetes Go
"解锁K8s二开新姿势!client-go:你不可不知的Go语言神器,让Kubernetes集群管理如虎添翼,秒变运维大神!"
【8月更文挑战第14天】随着云原生技术的发展,Kubernetes (K8s) 成为容器编排的首选。client-go作为K8s的官方Go语言客户端库,通过封装RESTful API,使开发者能便捷地管理集群资源,如Pods和服务。本文介绍client-go基本概念、使用方法及自定义操作。涵盖ClientSet、DynamicClient等客户端实现,以及lister、informer等组件,通过示例展示如何列出集群中的所有Pods。client-go的强大功能助力高效开发和运维。
906 1
|
SQL 关系型数据库 Go
Golang ORM框架介绍及比较
Golang ORM框架介绍及比较
|
中间件 Go
Go如何优雅的记录操作日志
这篇文章比较硬核,会涉及到这几个知识点:协程、协程池、钩子函数、中间件以及异步方法的使用,文章最后会带大家去阅读Async的源码,了解其底层实现。
2220 0
Go如何优雅的记录操作日志