https://github.com/tsak/concurrent-csv-writer
Thread-safe concurrent CSV writer for Go
https://github.com/tsak/concurrent-csv-writer
csv-writer go golang multi-threading thread-safe
Last synced: 26 days ago
JSON representation
Thread-safe concurrent CSV writer for Go
- Host: GitHub
- URL: https://github.com/tsak/concurrent-csv-writer
- Owner: tsak
- License: mit
- Created: 2017-08-24T12:05:47.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-12-17T23:14:49.000Z (about 4 years ago)
- Last Synced: 2024-06-19T17:51:20.329Z (over 1 year ago)
- Topics: csv-writer, go, golang, multi-threading, thread-safe
- Language: Go
- Size: 10.7 KB
- Stars: 13
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Concurrent CSV writer
[](https://goreportcard.com/report/github.com/tsak/concurrent-csv-writer)
[](https://godoc.org/github.com/tsak/concurrent-csv-writer)
A thread-safe way of concurrent writes to a CSV file in Go. Order of rows is **NOT** guaranteed.
Inspired by a [blog post](http://www.markhneedham.com/blog/2017/01/31/go-multi-threaded-writing-csv-file/) by [Mark Needham](http://www.markhneedham.com).
## Usage
```go
import "github.com/tsak/concurrent-csv-writer"
```
```bash
go get "github.com/tsak/concurrent-csv-writer"
```
## Example
```go
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
}
}
```
### Output
Notice the lack of order of entries.
```csv
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
...
```
## Benchmark
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
```
## License
[MIT](https://github.com/tsak/concurrent-csv-writer/blob/master/LICENSE)