Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dsalcedo/5967c1a11de1c4e9339fe220a5342f2b to your computer and use it in GitHub Desktop.
Save dsalcedo/5967c1a11de1c4e9339fe220a5342f2b to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"log"
"os"
)
var concurrency = 100
func main() {
// This channel has no buffer, so it only accepts input when something is ready
// to take it out. This keeps the reading from getting ahead of the writers.
workQueue := make(chan string)
// We need to know when everyone is done so we can exit.
complete := make(chan bool)
// Read the lines into the work queue.
go func() {
file, err := os.Open("/path/to/file.csv")
if err != nil {
log.Fatal(err)
}
// Close when the functin returns
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
workQueue <- scanner.Text()
}
// Close the channel so everyone reading from it knows we're done.
close(workQueue)
}()
// Now read them all off, concurrently.
for i := 0; i < concurrency; i++ {
go startWorking(workQueue, complete)
}
// Wait for everyone to finish.
for i := 0; i < concurrency; i++ {
<-complete
}
}
func startWorking(queue chan string, complete chan bool) {
for line := range queue {
// Do the work with the line.
}
// Let the main process know we're done.
complete <- true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment