golang[67]-go语言生成文档注释

写文档注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package queue

// A FIFO queue.
type Queue []int

// Pushes the element into the queue.
// e.g. q.Push(123)
func (q *Queue) Push(v int) {
*q = append(*q, v)
}

// Pops element from head.
func (q *Queue) Pop() int {
head := (*q)[0]
*q = (*q)[1:]
return head
}

// Returns if the queue is empty or not.
func (q *Queue) IsEmpty() bool {
return len(*q) == 0
}

查看文档

1
go doc

生成文档

1
godoc -http :6060

生成文档注释

queue_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package queue

import "fmt"

func ExampleQueue_Pop() {
q := Queue{1}
q.Push(2)
q.Push(3)
fmt.Println(q.Pop())
fmt.Println(q.Pop())
fmt.Println(q.IsEmpty())

fmt.Println(q.Pop())
fmt.Println(q.IsEmpty())

// Output:
// 1
// 2
// false
// 3
// true
}