In my application, I listen to nats and the output I get is a map with data. I need to generate a daily csv file using the data from the map. Now the data from the map is written to the file continuously. How can I write data to the file only at the end of each day?
type Hits struct {
HitMap map[string]map[SearchRequestKey]uint32
Mu sync.RWMutex
Log zerolog.Logger
TimeStart time.Time
MinHits int
WasDelete map[string]int64
Cnt1 float32
Version int
QQMap map[string]struct{}
GitlabToken string
}
type SearchRequestKey struct {
Query string
Date string
}
func FileAdd(hits *models.Hits) {
file, err := os.OpenFile("query-hits.csv", os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Println(err)
}
writer := csv.NewWriter(file)
writer.Comma = '|'
for key, value := range hits.HitMap {
for query, shard := range value {
err := writer.Write([]string{query.Query, key, strconv.Itoa(int(shard))})
if err != nil {
fmt.Println(err)
}
}
}
}