-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_simple_test.go
57 lines (47 loc) · 1006 Bytes
/
example_simple_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
package csvdecoder_test
import (
"fmt"
"os"
"github.com/stefantds/csvdecoder"
)
type User struct {
Name string
Active bool
Age int
}
func Example_simple() {
// the csv file contains the values:
//john,44,true
//lucy,48,false
//mr hyde,34,true
file, err := os.Open("./example_data/simple.csv")
if err != nil {
// handle error
return
}
defer file.Close()
// create a new decoder that will read from the given file
decoder, err := csvdecoder.New(file)
if err != nil {
// handle error
return
}
// iterate over the rows in the file
for decoder.Next() {
var u User
// scan the first three values in the name, age and active fields respectively
if err := decoder.Scan(&u.Name, &u.Age, &u.Active); err != nil {
// handle error
return
}
fmt.Println(u)
}
// check if the loop stopped prematurely because of an error
if err = decoder.Err(); err != nil {
// handle error
return
}
// Output: {john true 44}
// {lucy false 48}
// {mr hyde true 34}
}