Skip to content

tsak/concurrent-csv-writer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Concurrent CSV writer

Go Report Card GoDoc

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.

Usage

import "github.com/tsak/concurrent-csv-writer"
go get "github.com/tsak/concurrent-csv-writer"

Example

    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.

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