Skip to content

Commit 30652f0

Browse files
committed
Add examples to csvx
1 parent 8129aa4 commit 30652f0

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

csvx/decode.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ import (
7171
"strings"
7272
)
7373

74-
// TODO(amit): Add examples.
75-
// TODO(amit): Change to 1-based column index?
7674
// TODO(amit): Struct pointers?
7775
// TODO(amit): Handle slices?
7876

csvx/examples_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package csvx
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func ExampleDecodeReader() {
9+
type person struct {
10+
Name string
11+
Age int
12+
}
13+
input := strings.NewReader("alice,30\nbob,25")
14+
15+
for p, err := range DecodeReader[person](input) {
16+
if err != nil {
17+
panic(err)
18+
}
19+
fmt.Println(p.Name, "is", p.Age, "years old")
20+
}
21+
22+
//Output:
23+
//alice is 30 years old
24+
//bob is 25 years old
25+
}
26+
27+
func ExampleDecodeReaderHeader() {
28+
type person struct {
29+
Name string
30+
Age int
31+
}
32+
input := strings.NewReader(
33+
"user_id,age,city,name\n" +
34+
"111,30,paris,alice\n" +
35+
"222,25,london,bob")
36+
37+
for p, err := range DecodeReaderHeader[person](input) {
38+
if err != nil {
39+
panic(err)
40+
}
41+
fmt.Println(p.Name, "is", p.Age, "years old")
42+
}
43+
44+
//Output:
45+
//alice is 30 years old
46+
//bob is 25 years old
47+
}

0 commit comments

Comments
 (0)