godoc 技巧与注意事项

简介:

文档对于代码的意义不用多说。在golang bolg中已经给出了详细的描述http://blog.golang.org/godoc-documenting-go-code

我在实战中踩到了不少坑,这里给出更详细的解释以及注意事项。

我们针对golang源码中的注释进行分析得到如下结果。

针对Package的文档

Synopsis

参考http://golang.org/pkg/中的Synopsis。这句话主要出现在针对Package注释中的开头位置。

OverView

参考http://golang.org/pkg/archive/tar/。是针对Package中的注释出现的。如果出现连接,无需标注,生成文档的时候自动会处理成连接

参考例子与注意事项

包: [$GOROOT/src/encoding/json]

文件:encode.go

 
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package json implements encoding and decoding of JSON objects as defined in
  5. // RFC 4627. The mapping between JSON objects and Go values is described
  6. // in the documentation for the Marshal and Unmarshal functions.
  7. //
  8. // See "JSON and Go" for an introduction to this package:
  9. // http://golang.org/doc/articles/json_and_go.html
  10. package json

从注释中可以看出第四行是断开的,从第四行开始到package json都为针对包的注释。

目录中Synopsis出现内容为:Package json implements encoding and decoding of JSON objects as defined in RFC 4627.

参考注意事项:

  1. 在代码的package上面
  2. 在上面不能有空行
  3. 注释不能断开(中间不能有空行)
  4. 最前面一句话会模块的summary会出现在package index中
  5. 第一句话以及之后的内容会出现在OverView中

对比文件:decode.go

 
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Represents JSON data structure using native Go types: booleans, floats,
  5. // strings, arrays, and maps.
  6. package json

在package上面有空行,因此只是针对文件的注释不显示在godoc中。

针对Function

例子:

 
  1. // Marshaler is the interface implemented by objects that
  2. // can marshal themselves into valid JSON.
  3. type Marshaler interface {
  4. MarshalJSON() ([]byte, error)
  5. }

我们可以看到:

  1. 在函数上面进行注释
  2. 中间不能有空行
  3. 开始需要 [空格]FunctionName[空格] Summary
  4. 然后继续说明
  5. 想圈起来说明参数: 加缩进

进阶技巧:

例子同理于:Function Package

 
  1. // Marshaler is the interface implemented by objects that
  2. /*
  3. can marshal themselves into valid JSON.
  4. */
  5. type Marshaler interface {
  6. MarshalJSON() ([]byte, error)
  7. }

这样不算断开,写文档的时候就方便多了。

针对BUG

 
  1. // BUG(src): Mapping between XML elements and data structures is inherently flawed:
  2. // an XML element is an order-dependent collection of anonymous
  3. // values, while a data structure is an order-independent collection
  4. // of named values.
  5. // See package json for a textual representation more suitable
  6. // to data structures.

godoc会先查找:[空格]BUG

然后显示在Package说明文档最下面,例子:http://golang.org/pkg/encoding/xml/

针对Example

  1. 文件名惯用:example_test.go(其他也可以)
  2. 包名: apckage_test
  3. 方法名:
    • OverView中: Example
    • 方法中: Example[FuncName]
    • 方法中+一些模式:Example[FuncName]_[Mod]

例子查看:http://golang.org/pkg/errors/

Example文件(example_test.go):

 
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package errors_test
  5. import (
  6. "fmt"
  7. "time"
  8. )
  9. // MyError is an error implementation that includes a time and message.
  10. type MyError struct {
  11. When time.Time
  12. What string
  13. }
  14. func (e MyError) Error() string {
  15. return fmt.Sprintf("%v: %v", e.When, e.What)
  16. }
  17. func oops() error {
  18. return MyError{
  19. time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
  20. "the file system has gone away",
  21. }
  22. }
  23. func Example() {
  24. if err := oops(); err != nil {
  25. fmt.Println(err)
  26. }
  27. // Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away
  28. }
  1. 注意文件名为:example_test.go
  2. 注意package名为 errors_test
  3. 针对Function的注释会出现在网页的Example中
  4. 如果函数名直接叫Example会直接显示在OverView中

参考文件(errors_test.go):

 
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package errors_test
  5. import (
  6. "errors"
  7. "fmt"
  8. "testing"
  9. )
  10. func TestNewEqual(t *testing.T) {
  11. // Different allocations should not be equal.
  12. if errors.New("abc") == errors.New("abc") {
  13. t.Errorf(`New("abc") == New("abc")`)
  14. }
  15. if errors.New("abc") == errors.New("xyz") {
  16. t.Errorf(`New("abc") == New("xyz")`)
  17. }
  18. // Same allocation should be equal to itself (not crash).
  19. err := errors.New("jkl")
  20. if err != err {
  21. t.Errorf(`err != err`)
  22. }
  23. }
  24. func TestErrorMethod(t *testing.T) {
  25. err := errors.New("abc")
  26. if err.Error() != "abc" {
  27. t.Errorf(`New("abc").Error() = %q, want %q`, err.Error(), "abc")
  28. }
  29. }
  30. func ExampleNew() {
  31. err := errors.New("emit macho dwarf: elf header corrupted")
  32. if err != nil {
  33. fmt.Print(err)
  34. }
  35. // Output: emit macho dwarf: elf header corrupted
  36. }
  37. // The fmt package's Errorf function lets us use the package's formatting
  38. // features to create descriptive error messages.
  39. func ExampleNew_errorf() {
  40. const name, id = "bimmler", 17
  41. err := fmt.Errorf("user %q (id %d) not found", name, id)
  42. if err != nil {
  43. fmt.Print(err)
  44. }
  45. // Output: user "bimmler" (id 17) not found
  46. }
  1. ExampleNew就是针对New的例子
  2. ExampleNew_errorf 给例子加名字详细效果可以查看这里

针对godoc命令

我常用两种方式:

  1. godoc -http=:6060 直接运行网页上的版本,很方便
  2. godoc package [name ...] 在开发的时候文档速查

总结

一般工程中搞定这些基本就够了。详细的还是要动手做一做。

我没搞定的:怎么能显示成Main函数的,并且能跑Goplayground

原文发布时间为:2015-07-11




本文来自云栖社区合作伙伴“Linux中国

目录
相关文章
|
6月前
|
存储 算法 测试技术
关于HOperatorSet.CountChannels的注意事项
关于HOperatorSet.CountChannels的注意事项
|
2月前
|
前端开发 JavaScript API
reactAPI讲解以及注意事项
reactAPI讲解以及注意事项
12 2
|
网络协议
AFNetWork3.0使用注意事项
AFNetWork3.0使用注意事项
95 0
|
Java 数据安全/隐私保护 开发者
函数的注意事项| 学习笔记
快速学习函数的注意事项
116 0
|
Python
函数注意事项
# 函数的位置参数必须要传实参,可以按位置,也可以按关键字传 # 函数的默认参数可以不传实参,可以按位置,也可以按关键字 # 不定长参数*args只收集位置参数形成元组,不定长参数应放在后面,要不会把实参当做位置参数然后报错 # 用**,只要定义了关键字参数,以后针对这个参数传值就必须是关键字形式...
960 0
|
关系型数据库 PostgreSQL

热门文章

最新文章