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 // 手动安装

> 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:\Go\src
godoc -http=:6060
扫描文件需要时间,启动之后有时候还要等一会。
访问浏览器 http://localhost:6060/
在这里插入图片描述
访问指定的包
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加上注释,说明此包的作用,例如

// 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区域

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

关于DEPRECATED弃用

// 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
}

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

  example:
      // 123
      //  123

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

> 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
...
...
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值