-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
95 lines (81 loc) · 1.43 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
package dsvreader_test
import (
"bytes"
"fmt"
"github.com/cristalhq/dsvreader"
)
func ExampleReader() {
bs := bytes.NewBufferString(
`foo 42
bar 123
`)
r := dsvreader.NewTSV(bs)
for r.Next() {
col1 := r.String()
col2 := r.Int()
fmt.Printf("col1=%s, col2=%d\n", col1, col2)
}
if err := r.Error(); err != nil {
fmt.Printf("unexpected error: %s", err)
}
// Output:
// col1=foo, col2=42
// col1=bar, col2=123
}
func ExampleReader_HasCols() {
bs := bytes.NewBufferString(
"foo\n" +
"bar\tbaz\n" +
"\n" +
"a\tb\tc\n")
r := dsvreader.NewTSV(bs)
for r.Next() {
for r.HasCols() {
s := r.String()
fmt.Printf("%q,", s)
}
fmt.Printf("\n")
}
if err := r.Error(); err != nil {
fmt.Printf("unexpected error: %s", err)
}
// Output:
// "foo",
// "bar","baz",
//
// "a","b","c",
}
func ExampleReader_Next() {
bs := bytes.NewBufferString("1\n2\n3\n42\n")
r := dsvreader.NewTSV(bs)
for r.Next() {
n := r.Int()
fmt.Printf("%d\n", n)
}
if err := r.Error(); err != nil {
fmt.Printf("unexpected error: %s", err)
}
// Output:
// 1
// 2
// 3
// 42
}
func Example_csvReader() {
bs := bytes.NewBufferString(
`foo,42
bar,123
`)
r := dsvreader.NewCSV(bs)
for r.Next() {
col1 := r.String()
col2 := r.Int()
fmt.Printf("col1=%s, col2=%d\n", col1, col2)
}
if err := r.Error(); err != nil {
fmt.Printf("unexpected error: %s", err)
}
// Output:
// col1=foo, col2=42
// col1=bar, col2=123
}