-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.go
40 lines (36 loc) · 907 Bytes
/
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
package subscriptionparser
import (
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
)
func WriteCSV(subscriptions []Subscriptions, filename string) (err error) {
file, err := os.Create(filename + ".csv")
if err != nil {
return err
}
defer file.Close()
w := csv.NewWriter(file)
defer w.Flush()
headerLine := []string{"Subscriber", "Menge", "Artikelnr", "Artikel", "Endkunden-Preis"}
if err := w.Write(headerLine); err != nil {
log.Fatalln("error writing record to csv:", err)
}
for _, record := range subscriptions {
line := []string{}
for _, products := range record.Products {
line = []string{
record.Subscriber,
strconv.Itoa(products.Menge),
products.Artikel.ArticleNr,
products.Artikel.Description,
fmt.Sprintf("%f", products.EndkundenPreis)}
}
if err := w.Write(line); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
return err
}