-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
58 lines (49 loc) · 859 Bytes
/
writer.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
package main
import (
"encoding/csv"
"log"
"os"
)
// Row as written to the CSV
type Row struct {
Category string
Name string
Title string
Description string
Notes string
}
func (s *Service) newWriter(filename string) {
s.wg.Add(1)
defer s.wg.Done()
file, err := os.Create(filename + ".csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
s.writer = csv.NewWriter(file)
defer s.writer.Flush()
err = s.writer.Write([]string{
"Category", "Name", "Title", "Description", "Notes"})
if err != nil {
log.Println(err)
return
}
s.save = make(chan Row)
for {
row, more := <-s.save
if !more {
// channel closed, stop writer
return
}
err = s.writer.Write([]string{
row.Category,
row.Name,
row.Title,
row.Description,
row.Notes,
})
if err != nil {
log.Fatal(err)
}
}
}