Chinaunix首页 | 论坛 | 博客
  • 博客访问: 304215
  • 博文数量: 21
  • 博客积分: 250
  • 博客等级: 二等列兵
  • 技术积分: 484
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-06 23:10
个人简介

程序猿

文章分类

全部博文(21)

文章存档

2016年(17)

2014年(3)

2013年(1)

分类: LINUX

2016-04-24 16:42:05

查看文档

golang提供了godoc命令方便我们查看文档。文档是由注释自动生成的。
godoc有两种查看文档的方式:网页方式和命令行方式 。


网页方式

想要通过网页访问go文档,最简单的方法就是访问,但是如果你不能上网(翻墙)或者想看自定义包的文档,就必须在本地建立http文档服务器。

运行如下命令启动godoc的http server
  1. godoc -http=:6060
然后你就能通过浏览器访问127.0.0.1:6060查看go文档。


命令行方式

直接运行
  1. godoc [package] ...
例如
  1. godoc fmt //查看fmt包
  2. godoc fmt Printf //查看fmt包中的Printf函数

需要注意的是,godoc默认情况下只会在GOROOT和GOPATH路径下搜索,可以使用-goroot指定搜索路径。

godoc -q 可以搜索所有"string"字符串出现的地方。

  1. # godoc -q Reader


  2. QUERY
  3.         Reader

  4. DID YOU MEAN

  5.         reader

  6. Types
  7.     strings.Reader
  8.     bytes.Reader
  9.     io.Reader
  10.     bufio.Reader
  11.     net/textproto.Reader
  12.     debug/dwarf.Reader
  13.     compress/flate.Reader
  14.     compress/gzip.Reader
  15.     mime/multipart.Reader
  16.     archive/zip.Reader
  17.     image/jpeg.Reader
  18.     archive/tar.Reader
  19.     encoding/csv.Reader
  20.     mime/quotedprintable.Reader

  21. Variables
  22.     crypto/rand.Reader

  23. PACKAGE-LEVEL DECLARATIONS

  24. package bufio
  25.         /src/bufio/bufio.go:31

  26. ...


编写自己的文档

前面说过,golang会把注释自动变成文档,这就需要我们在编写代码时注意注释的写法。

对于想变成文档的注释,需要放在被注释内容的前面,且注释中间不能有空行。
对于package,注释的第一句会出现在package索引的Synopsis中。
例如:
  1. // Copyright 2016 vv.
  2. // This should not be shown in godoc.

  3. // This is an example project for godoc.
  4. package hello
godoc对应的部分
  1. PACKAGE DOCUMENTATION

  2. package hello
  3.     import "github.com/vv1133/golang_example/doc/hello"

  4.     This is an example project for godoc.
可以看到,由于第三行是空行,前面两行就没有出现在文档中。

对于程序中已知且还未修改的bug,可以使用BUG来注释,这些内容会出现在文档的结尾。
如:
  1. // BUG(r): Dog SetAge has no effect.
对应godoc生成
  1. BUGS

  2.    Dog SetAge has no effect.

附:完整的代码和文档

代码
  1. // Copyright 2016 vv.
  2. // This should not be shown in godoc.

  3. // This is an example project for godoc.
  4. package hello

  5. // Animal is the interface implemented by any
  6. // value that has an Age() method.
  7. type Animal interface {
  8. // Age returns the value of animal's age.
  9. Age() int
  10. // SetAge sets the age of this animal.
  11. SetAge(int)
  12. }

  13. // Cat infomation.
  14. type Cat struct {
  15. age int
  16. }

  17. // Dog infomation.
  18. type Dog struct {
  19. age int
  20. }

  21. func (this Cat) Age() int {
  22. return this.age
  23. }

  24. func (this *Cat) SetAge(age int) {
  25. this.age = age
  26. }

  27. func (this Dog) Age() int {
  28. return this.age
  29. }

  30. // BUG(r): Dog SetAge has no effect.

  31. func (this Dog) SetAge(age int) {
  32. this.age = age
  33. }

生成的文档
  1. PACKAGE DOCUMENTATION

  2. package hello
  3.     import "github.com/vv1133/golang_example/doc/hello"

  4.     This is an example project for godoc.

  5. TYPES

  6. type Animal interface {
  7.     // Age returns the value of animal's age.
  8.      Age() int
  9.     // SetAge sets the age of this animal.
  10.     SetAge(int)
  11. }
  12.     Animal is the interface implemented by any value that has an Age()
  13.     method.

  14. type Cat struct {
  15.     // contains filtered or unexported fields
  16. }
  17.     Cat infomation.

  18. func (this Cat) Age() int

  19. func (this *Cat) SetAge(age int)

  20. type Dog struct {
  21.     // contains filtered or unexported fields
  22. }
  23.     Dog infomation.

  24. func (this Dog) Age() int

  25. func (this Dog) SetAge(age int)



  26. BUGS

  27.     Dog SetAge has no effect.

阅读(4409) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

不懂IT2016-06-05 23:34:08

写这个的时候你一定很有成就感,语言里透漏出对这种技能的熟掌握和喜悦