forked from gocarina/gocsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe_csv.go
41 lines (35 loc) · 808 Bytes
/
safe_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
package gocsv
//Wraps around SafeCSVWriter and makes it thread safe.
import (
"encoding/csv"
"sync"
)
//CSVWriter interface for anything implementing csv writing api
type CSVWriter interface {
Write(row []string) error
Flush()
Error() error
}
//SafeCSVWriter mutex protected thread safe csv writer
type SafeCSVWriter struct {
*csv.Writer
m sync.Mutex
}
//NewSafeCSVWriter create a new SafeCSVWriter
func NewSafeCSVWriter(original *csv.Writer) *SafeCSVWriter {
return &SafeCSVWriter{
Writer: original,
}
}
//Write the csv writer in a threadsafe way
func (w *SafeCSVWriter) Write(row []string) error {
w.m.Lock()
defer w.m.Unlock()
return w.Writer.Write(row)
}
//Flush flush the csv writer in a threadsafe way
func (w *SafeCSVWriter) Flush() {
w.m.Lock()
w.Writer.Flush()
w.m.Unlock()
}