A thread-safe way of concurrent writes to a CSV file in Go. Order of rows is NOT guaranteed.
Inspired by a blog post by Mark Needham.
import "github.com/tsak/concurrent-csv-writer"
go get "github.com/tsak/concurrent-csv-writer"
package main
import (
"github.com/tsak/concurrent-csv-writer"
"strconv"
)
func main() {
// Create `sample.csv` in current directory
csv, err := ccsv.NewCsvWriter("sample.csv")
if err != nil {
panic("Could not open `sample.csv` for writing")
}
// Flush pending writes and close file upon exit of main()
defer csv.Close()
count := 99
done := make(chan bool)
for i := count; i > 0; i-- {
go func(i int) {
csv.Write([]string{strconv.Itoa(i), "bottles", "of", "beer"})
done <- true
}(i)
}
for i := 0; i < count; i++ {
<-done
}
}
Notice the lack of order of entries.
98,bottles,of,beer
90,bottles,of,beer
89,bottles,of,beer
93,bottles,of,beer
97,bottles,of,beer
96,bottles,of,beer
95,bottles,of,beer
94,bottles,of,beer
99,bottles,of,beer
92,bottles,of,beer
...
Running go test -bench .
goos: linux
goarch: amd64
pkg: github.com/tsak/concurrent-csv-writer
BenchmarkCsvWriter_Write/nil-slice-8 890214 1329 ns/op
BenchmarkCsvWriter_Write/row-8 816813 1471 ns/op
PASS
ok github.com/tsak/concurrent-csv-writer 3.954s