Refactoring of saving to csv

This commit is contained in:
2016-03-27 02:41:12 +03:00
parent 05d305295a
commit 64c02ca343

View File

@@ -3,16 +3,17 @@ package main
import ( import (
"encoding/csv" "encoding/csv"
"io"
"log" "log"
"os" "os"
"sort" "sort"
"strconv" "strconv"
) )
func SaveToCsvFile(data []StatForTime, keymap map[uint8]string, path string, isOnlySum bool) { func SaveToCsvWriter(data []StatForTime, keyMap map[uint8]string, writerOut io.Writer, isOnlySum bool) {
numKeysInt := make([]int, 0) numKeysInt := make([]int, 0)
for key := range keymap { for key := range keyMap {
numKeysInt = append(numKeysInt, int(key)) numKeysInt = append(numKeysInt, int(key))
} }
sort.Ints(numKeysInt) sort.Ints(numKeysInt)
@@ -25,7 +26,7 @@ func SaveToCsvFile(data []StatForTime, keymap map[uint8]string, path string, isO
titleLine = append(titleLine, "Time") titleLine = append(titleLine, "Time")
if !isOnlySum { if !isOnlySum {
for _, key := range numKeys { for _, key := range numKeys {
titleLine = append(titleLine, keymap[key]) titleLine = append(titleLine, keyMap[key])
} }
} }
titleLine = append(titleLine, "Sum") titleLine = append(titleLine, "Sum")
@@ -46,12 +47,17 @@ func SaveToCsvFile(data []StatForTime, keymap map[uint8]string, path string, isO
table = append(table, line) table = append(table, line)
} }
writer := csv.NewWriter(writerOut)
writer.WriteAll(table)
}
func SaveToCsvFile(data []StatForTime, keyMap map[uint8]string, path string, isOnlySum bool) {
csvfile, err := os.Create(path) csvfile, err := os.Create(path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
defer csvfile.Close() defer csvfile.Close()
writer := csv.NewWriter(csvfile)
writer.WriteAll(table) SaveToCsvWriter(data, keyMap, csvfile, isOnlySum)
} }