forked from jweir/csv
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
47 lines (35 loc) · 825 Bytes
/
example_test.go
File metadata and controls
47 lines (35 loc) · 825 Bytes
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
package csv
import (
"fmt"
)
type Person struct {
Name string `csv:"Full Name"`
income string // unexported fields are not Unmarshalled
Age int `csv:"-"` // skip this field
Address Address `csv:"Street"` // skip this field
}
type Address struct {
City string
Street string
}
func (a *Address) UnmarshalCSV(val string, row *Row) error {
c, _ := row.Named("City")
s, _ := row.Named("Street")
a.Street = s
a.City = c
return nil
}
func ExampleUnmarshal() {
people := []Person{}
sample := []byte(
`Full Name,income,Age,City,Street
John Doe,"32,000",45,Brooklyn,"7th Street"
`)
err := Unmarshal(sample, &people)
if err != nil {
fmt.Println("Error: ", err)
}
fmt.Printf("%+v", people)
// Output:
// [{Name:John Doe income: Age:0 Address:{City:Brooklyn Street:7th Street}}]
}