blob: 4b2882f1c425943523c8126c1e417064b29406ad (
plain)
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
|
package gocsv
//Wraps around SafeCSVWriter and makes it thread safe.
import (
"encoding/csv"
"sync"
)
type SafeCSVWriter struct {
*csv.Writer
m sync.Mutex
}
func NewSafeCSVWriter(original *csv.Writer) *SafeCSVWriter {
return &SafeCSVWriter{
Writer: original,
}
}
//Override write
func (w *SafeCSVWriter) Write(row []string) error {
w.m.Lock()
defer w.m.Unlock()
return w.Writer.Write(row)
}
//Override flush
func (w *SafeCSVWriter) Flush() {
w.m.Lock()
w.Writer.Flush()
w.m.Unlock()
}
|