返回首页 Go 入门指南

第一部分:学习 Go 语言

第二部分:语言的核心结构与技术

第4章:基本结构和基本数据类型

Map

第三部分:Go 高级编程

9.6 为自定义包使用 godoc

godoc工具(第 3.6 节)在显示自定义包中的注释也有很好的效果:注释必须以 // 开始并无空行放在声明(包,类型,函数)前。godoc 会为每个文件生成一系列的网页。

例如:

  • 在 do_examples 目录下我们有第 11.7 节中的用来排序的 go 文件,文件中有一些注释(文件需要未编译)
  • 命令行下进入目录下并输入命令:

    godoc -http =:6060 -goroot="."

. 是指当前目录,-goroot 参数可以是 /path/to/my/package1 这样的形式指出 package1 在你源码中的位置或接受用冒号形式分隔的路径,无根目录的路径为相对于当前目录的相对路径)

然后你会看到本地的 godoc 页面(详见第 3.6 节)从左到右一次显示出目录中的包:

doc_example:

doc_example | Packages | Commands | Specification

下面是链接到源码和所有对象时有序概述(所以是很好的浏览和查找源代码的方式),连同文件/注释:

sort 包

func Float64sAreSorted

type IntArray

func IntsAreSortedfunc IsSortedfunc Sort

func (IntArray) Len

func SortFloat64s

func (IntArray) Less

func SortInts

func (IntArray) Swap

func SortStrings type Interface

func StringsAreSorted type StringArray type Float64Array

func (StringArray) Len

func (Float64Array) Len

func (StringArray) Less

func (Float64Array) Less

func (StringArray) Swap

func (Float64Array) Swap

// Other packages
import "doc_example" 

使用通用的接口排序:

func Float64sAreSorted[Top]
func Float64sAreSorted(a []float64) bool

func IntsAreSorted[Top]
func IntsAreSorted(a []int) bool

func IsSorted[Top]
func IsSorted(data Interface) bool
Test if data is sorted

func Sort[Top]
func Sort(data Interface)
General sort function

func SortInts[Top]
func SortInts(a []int)

Convenience wrappers for common cases: type IntArray[Top]
Convenience types for common cases: IntArray type IntArray []int  

如果你在一个团队中工作,并在源代码树被存储在网络硬盘上,就可以使用 godoc 给所有团队成员连续文档的支持。通过设置 sync_minutes=n,你甚至可以让它每 n 分钟自动更新您的文档!

链接