safe_csv.go 504 B

1234567891011121314151617181920212223242526272829303132
  1. package gocsv
  2. //Wraps around SafeCSVWriter and makes it thread safe.
  3. import (
  4. "encoding/csv"
  5. "sync"
  6. )
  7. type SafeCSVWriter struct {
  8. *csv.Writer
  9. m sync.Mutex
  10. }
  11. func NewSafeCSVWriter(original *csv.Writer) *SafeCSVWriter {
  12. return &SafeCSVWriter{
  13. Writer: original,
  14. }
  15. }
  16. //Override write
  17. func (w *SafeCSVWriter) Write(row []string) error {
  18. w.m.Lock()
  19. defer w.m.Unlock()
  20. return w.Writer.Write(row)
  21. }
  22. //Override flush
  23. func (w *SafeCSVWriter) Flush() {
  24. w.m.Lock()
  25. w.Writer.Flush()
  26. w.m.Unlock()
  27. }