-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
146 lines (133 loc) · 3.97 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package srcutil_test
import (
"fmt"
"go/doc"
"log"
"strings"
"github.com/cstockton/go-srcutil"
)
func Example() {
pkg, err := srcutil.Import("io")
if err != nil {
log.Fatal(err)
}
fmt.Printf("// %s: %s\n", pkg, pkg.Doc)
vars := pkg.Vars()
for _, v := range vars {
fmt.Printf("var %v %v\n", v.Name(), v.Type())
}
// Output:
// // io: Package io provides basic interfaces to I/O primitives.
// var EOF error
// var ErrClosedPipe error
// var ErrNoProgress error
// var ErrShortBuffer error
// var ErrShortWrite error
// var ErrUnexpectedEOF error
}
func ExamplePackage_Docs() {
pkg, err := srcutil.Import("io")
if err != nil {
log.Fatal(err)
}
docs := pkg.Docs()
consts := docs.Consts()
fmt.Printf("// %v", consts[0].Doc)
fmt.Printf("const(\n %v\n)\n\n", strings.Join(consts[0].Names, "\n "))
vars := docs.Vars()
for _, v := range vars {
fmt.Printf("// %v", doc.Synopsis(consts[0].Doc))
fmt.Printf("var %v\n", v.Names[0])
}
fmt.Print("\n")
types := docs.Types()
for _, typ := range types {
if strings.Contains(typ.Name, "Reader") {
fmt.Printf("// %v\n", doc.Synopsis(typ.Doc))
for _, f := range typ.Funcs {
fmt.Printf("// %v\n", doc.Synopsis(f.Doc))
}
}
}
// Output:
// // Seek whence values.
// const(
// SeekStart
// SeekCurrent
// SeekEnd
// )
//
// // Seek whence values.var EOF
// // Seek whence values.var ErrClosedPipe
// // Seek whence values.var ErrNoProgress
// // Seek whence values.var ErrShortBuffer
// // Seek whence values.var ErrShortWrite
// // Seek whence values.var ErrUnexpectedEOF
//
// // ByteReader is the interface that wraps the ReadByte method.
// // A LimitedReader reads from R but limits the amount of data returned to just N bytes.
// // A PipeReader is the read half of a pipe.
// // Pipe creates a synchronous in-memory pipe.
// // Reader is the interface that wraps the basic Read method.
// // LimitReader returns a Reader that reads from r but stops with EOF after n bytes.
// // MultiReader returns a Reader that's the logical concatenation of the provided input readers.
// // TeeReader returns a Reader that writes to w what it reads from r.
// // ReaderAt is the interface that wraps the basic ReadAt method.
// // ReaderFrom is the interface that wraps the ReadFrom method.
// // RuneReader is the interface that wraps the ReadRune method.
// // SectionReader implements Read, Seek, and ReadAt on a section of an underlying ReaderAt.
// // NewSectionReader returns a SectionReader that reads from r starting at offset off and stops with EOF after n bytes.
}
func ExamplePackage_Methods() {
pkg, err := srcutil.Import("bufio")
if err != nil {
log.Fatal(err)
}
pkgMethods := pkg.Methods()
printer := func(methodSet srcutil.MethodSet) {
fmt.Printf("type %v (%d methods)\n", methodSet.Name, methodSet.Len())
for _, name := range methodSet.Names() {
method := methodSet.Methods[name]
fmt.Printf(" %v%v\n returns %v\n", name, method.Params(), method.Results())
}
}
printer(pkgMethods["Reader"])
// Output:
// type Reader (18 methods)
// Buffered()
// returns (int)
// Discard(n int)
// returns (discarded int, err error)
// Peek(n int)
// returns ([]byte, error)
// Read(p []byte)
// returns (n int, err error)
// ReadByte()
// returns (byte, error)
// ReadBytes(delim byte)
// returns ([]byte, error)
// ReadLine()
// returns (line []byte, isPrefix bool, err error)
// ReadRune()
// returns (r rune, size int, err error)
// ReadSlice(delim byte)
// returns (line []byte, err error)
// ReadString(delim byte)
// returns (string, error)
// Reset(r io.Reader)
// returns ()
// UnreadByte()
// returns (error)
// UnreadRune()
// returns (error)
// WriteTo(w io.Writer)
// returns (n int64, err error)
// fill()
// returns ()
// readErr()
// returns (error)
// reset(buf []byte, r io.Reader)
// returns ()
// writeBuf(w io.Writer)
// returns (int64, error)
}