menu arrow_back 湛蓝安全空间 |狂野湛蓝,暴躁每天 chevron_right All_wiki chevron_right the-way-to-go_ZH_CN chevron_right eBook chevron_right 09.6.md
  • home 首页
  • brightness_4 暗黑模式
  • cloud
    xLIYhHS7e34ez7Ma
    cloud
    湛蓝安全
    code
    Github
    09.6.md
    2.56 KB / 2024-07-16 23:14:27
        # 9.6 为自定义包使用 godoc
    
    godoc 工具([第 3.6 节](03.6.md))在显示自定义包中的注释也有很好的效果:注释必须以 `//` 开始并无空行放在声明(包,类型,函数)前。godoc 会为每个文件生成一系列的网页。
    
    例如:
    
    - 在 [doc_examples](examples/chapter_9/doc_example) 目录下我们有[第 11.7 节](11.7.md)中的用来排序的 go 文件,文件中有一些注释(文件需要未编译)
    - 命令行下进入目录下并输入命令:
    
    	`godoc -http=:6060 -goroot="."`
    
    (`.` 是指当前目录,`-goroot` 参数可以是 `/path/to/my/package1` 这样的形式指出 `package1` 在你源码中的位置或接受用冒号形式分隔的路径,无根目录的路径为相对于当前目录的相对路径)
    
    - 在浏览器打开地址:http://localhost:6060
    
    然后你会看到本地的 godoc 页面(详见[第 3.6 节](03.6.md))从左到右一次显示出目录中的包:
    
    ```
    doc_example:
    
    doc_example | Packages | Commands | Specification
    ```
    
    下面是链接到源码和所有对象时有序概述(所以是很好的浏览和查找源代码的方式),连同文件/注释:
    
    `sort` 包
    
    ```go
    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` 分钟自动更新您的文档!
    
    ## 链接
    
    - [目录](directory.md)
    - 上一节:[自定义包和可见性](09.5.md)
    - 下一节:[使用 go install 安装自定义包](09.7.md)
    
    
    links
    file_download