-
Notifications
You must be signed in to change notification settings - Fork 1
/
example2.go
51 lines (42 loc) · 988 Bytes
/
example2.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
//go:build example
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/mattn/go-colorable"
"github.com/hymkor/csvi"
"github.com/hymkor/csvi/uncsv"
)
func main() {
source := `01,02,03,04
"11","21","31","41"
"42","42","42","42"`
cfg := &csvi.Config{
Mode: &uncsv.Mode{Comma: ','},
HeaderLines: 1,
ProtectHeader: true,
OnCellValidated: func(e *csvi.CellValidatedEvent) (string, error) {
// All cells must contain only numbers
_, err := strconv.ParseFloat(e.Text, 64)
if err != nil {
return "", err
}
return e.Text, nil
},
}
result, err := cfg.Edit(strings.NewReader(source), colorable.NewColorableStdout())
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// // env GOEXPERIMENT=rangefunc go run example
// for row := range result.Each {
// os.Stdout.Write(row.Rebuild(cfg.Mode))
// }
result.Each(func(row *uncsv.Row) bool {
os.Stdout.Write(row.Rebuild(cfg.Mode))
return true
})
}