脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - Golang中的godoc使用简介(推荐)

Golang中的godoc使用简介(推荐)

2022-10-31 10:26raoxiaoya Golang

Godoc是go语言的文档化工具,类似于文档化工具godoc,类似于Python的Docstring和Java的Javadoc,这篇文章主要介绍了Golang中的godoc使用简介,需要的朋友可以参考下

go doc简介

Godoc是go语言的文档化工具,类似于文档化工具godoc,类似于Python的Docstring和Java的Javadoc
Godoc通过解析包含注释的Go代码来生成HTML或文本类型的文档。

 

Golang中的godoc使用简介

学习go语法的同时为了方便查看对应的文档,不同的Go版本会有一些改动,所以,使用本地Go源码生成的文档显然更精确。

go在1.13之前是自带godoc的,之后的版本需要自行安装。

go get -u golang.org/x/tools/cmd/godoc // 并没有自动安装
go install golang.org/x/tools/cmd/godoc // 手动安装

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
> godoc -h
 
usage: godoc -http=localhost:6060
  -goroot string
        Go root directory (default "D:\Go")
  -http string
        HTTP service address (default "localhost:6060")
  -index
        enable search index
  -index_files string
        glob pattern specifying index files; if not empty, the index is read from these files in sorted order
  -index_interval duration
        interval of indexing; 0 for default (5m), negative to only index once at startup
  -index_throttle float
        index throttle value; 0.0 = no time allocated, 1.0 = full throttle (default 0.75)
  -links
        link identifiers to their declarations (default true)
  -maxresults int
        maximum number of full text search results shown (default 10000)
  -notes string
        regular expression matching note markers to show (default "BUG")
  -play
        enable playground
  -templates string
        load templates/JS/CSS from disk in this directory
  -timestamps
        show timestamps with directory listings
  -url string
        print HTML for named URL
  -v    verbose mode
  -write_index
        write index to a file; the file name must be specified with -index_files
  -zip string
        zip file providing the file system to serve; disabled if empty

进入到Go源码目录,D:Gosrc
godoc -http=:6060
扫描文件需要时间,启动之后有时候还要等一会。
访问浏览器 http://localhost:6060/

Golang中的godoc使用简介(推荐)

访问指定的包
http://localhost:6060/pkg/fmt/
http://localhost:6060/pkg/archive/tar/

可以通过godoc -url "http://localhost:6060/pkg/" > doc.html导出为静态的html单个文件,但是这个体验很差。

如果我们要查看一个第三方包的文档,比如 github.com/julienschmidt/httprouter
首先要进入它的源码目录
godoc -http=localhost:6060
第一部分依然是 Go Standard library,第二部分 Third party 才是 httprouter 的文档

如果我们要查看某个项目的文档也可以使用这种方式,然后我就发现我们自己写的项目大多没有什么注释,于是这就牵涉到注释的规范。

注释使用//加一个空格并且要紧跟着被注释对象的上方。

首先需要给package加上注释,说明此包的作用,例如

?
1
2
3
4
// Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer
// object, creating another object (Reader or Writer) that also implements
// the interface but provides buffering and some help for textual I/O.
package bufio

同一目录下的包可以由很多个文件组成,如果每个文件都有对package进行注释的话,godoc会自动将所有注释"按照文件名的字母数序"进行合并

在无效注释中以BUG(who)开头的注释, 将被识别为已知bug, 显示在bugs区域

?
1
// BUG(who): 因为前面有BUG(who)这个关键字,所以这句注释就算没有紧跟关键字不会被隐藏掉

关于DEPRECATED弃用

?
1
2
3
4
5
// Time returns an int64 unix timestamp in milliseconds of the snowflake ID time
// DEPRECATED: the below function will be removed in a future release.
func (f ID) Time() int64 {
    return (int64(f) >> timeShift) + Epoch
}

在注释符要缩进的话,第二行注释符后面的空格要比上一行的空格多一个

?
1
2
3
example:
    // 123
    //  123

关于 go doc xxx
主要用来在终端上查看某个包的文档,它也是通过扫描包内的一些注释,然后格式化输出,但很少这么用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> go doc fmt
 
package fmt // import "fmt"
Package fmt implements formatted I/O with functions analogous to C's printf
and scanf. The format 'verbs' are derived from C's but are simpler.
Printing
The verbs:
General:
 
    %v  the value in a default format
        when printing structs, the plus flag (%+v) adds field names
    %#v a Go-syntax representation of the value
    %T  a Go-syntax representation of the type of the value
    %%  a literal percent sign; consumes no value
...
...

 

go doc约定规则

godoc
Go的注释规则很简单,为类型,变量,常量,函数或包编写注释时,直接在这些声明前编写普通形式的注释,中间不留空行即可。Godoc将这些注释与后面的声明连接到一起,达到文档化的目的。

1.注释符// 后加空格

?
1
2
// 这是一行注释
package main

2.注释紧邻关键字行写,否则不会被识别

?
1
2
3
4
5
6
7
8
9
// 不会被识别
// 会被识别
package main
// 不会被识别
 
// 会被识别1
// 会被识别2
func main(){
}

3.注释行的数量最好不要超过3行

4.已知bug注释:BUG(who)
godoc的输出将忽略那些与顶层声明不相邻的注释,只有一个例外,那就是以BUG(who)开头的注释。这些注释将被识别为已知的bug,并包含在文档的BUGS区。

?
1
2
3
4
// 注释
package main
// BUG(who)  //会被识别,放入文档的Bugs区
const a = 8

 

使用命令

首先在命令行进入所需要执行的.go文档目录
例:我的test.go放在C:/GoProject/src/test/ 目录下
需要在命令行进入C:/GoProject/src/test/
然后执行命令

?
1
godoc -http=:6060     //6060是godoc提供的默认端口

在浏览器输入 localhost:6060,即可进入godoc的网站,查看自己的文档需要在地址栏输入路径 localhost:6060/pkg/test

到此这篇关于Golang中的godoc使用简介的文章就介绍到这了,更多相关go语言godoc使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/raoxiaoya/article/details/127527287

延伸 · 阅读

精彩推荐
  • GolangGo定时器cron的使用详解

    Go定时器cron的使用详解

    本篇文章主要介绍了Go定时器cron的使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    骑头猪逛街2592020-05-13
  • Golanggo开发alertmanger实现钉钉报警

    go开发alertmanger实现钉钉报警

    本文主要介绍了go开发alertmanger实现钉钉报警,通过自己的url实现alertmanager的钉钉报警,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    luoguo5802021-08-15
  • Golang深入理解Golang Channel 的底层结构

    深入理解Golang Channel 的底层结构

    这篇文章主要介绍了深入理解Golang Channel 的底层结构,Go 语言的 channel 底层是什么数据结构?下面我们就一起来深入解析一下 channel,需要的朋友可以参考下...

    运维派3902022-08-29
  • Golanggolang字符串本质与原理详解

    golang字符串本质与原理详解

    这篇文章主要介绍了golang字符串本质与原理详解,golang中的字符串指的是所有8比特位字节字符串的集合,通常是UTF-8编码的文本,更多相关内容需要的小伙...

    ysj5722022-10-26
  • Golanggo语言入门环境搭建及GoLand安装教程详解

    go语言入门环境搭建及GoLand安装教程详解

    这篇文章主要介绍了go语言入门环境搭建及GoLand安装教程详解,需要的朋友可以参考下...

    ForFuture Group10522021-02-20
  • Golanggolang 随机数的两种方式

    golang 随机数的两种方式

    本文主要介绍了golang 随机数的两种方式,一种是伪随机,另一种是真随机,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习...

    @航空母舰6842022-07-05
  • Golanggolang package time的用法具体详解

    golang package time的用法具体详解

    本篇文章主要介绍了golang package time的用法具体详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    frluter6402020-05-15
  • Golanggolang struct 实现 interface的方法

    golang struct 实现 interface的方法

    这篇文章主要介绍了golang struct 实现 interface的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    sanerersan4762020-05-17