-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_to_csv.go
44 lines (37 loc) · 1001 Bytes
/
save_to_csv.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
package main
import (
"encoding/csv"
"fmt"
"os"
"time"
)
// save provides saving operation for source map.
func (c *config) save(source []map[string]string) error {
// Create a new file to collect CSV data.
outputFile, err := os.Create(fmt.Sprintf("%s/%d.csv", c.outputFolder, time.Now().Unix()))
if err != nil {
return err
}
defer func(outputFile *os.File) {
err := outputFile.Close()
if err != nil {
}
}(outputFile)
// Write the header of the CSV file and the successive rows by iterating through
// the JSON struct array.
writer := csv.NewWriter(outputFile)
defer writer.Flush()
// Write the header of the CSV file.
header := []string{c.contentField, "intent"}
if err = writer.Write(header); err != nil {
return err
}
// Write collected data to CSV file.
for _, r := range source {
// First column – content, second column – intent.
if err = writer.Write([]string{r["content"], c.qualify(r["content"])}); err != nil {
return err
}
}
return nil
}