Skip to content

Instantly share code, notes, and snippets.

@brydavis
Created June 15, 2019 00:49
Show Gist options
  • Save brydavis/e995b627fbfe8f3496d7f18034f35964 to your computer and use it in GitHub Desktop.
Save brydavis/e995b627fbfe8f3496d7f18034f35964 to your computer and use it in GitHub Desktop.
Read each file / each line concurrently
package main
import (
"bufio"
"fmt"
"os"
"sync"
"time"
)
var wg sync.WaitGroup
func main() {
start := time.Now()
filenames := []string{
"one.csv",
"two.csv",
"three.csv",
"four.csv",
"five.csv",
}
for n := range filenames {
wg.Add(1)
go func(fn string) {
defer wg.Done()
file, err := os.Open(fn)
if err != nil {
fmt.Println(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
wg.Add(1)
go process(scanner.Text())
}
}(filenames[n])
}
wg.Wait()
fmt.Printf("%v ns\n", time.Since(start).Nanoseconds())
}
func process(text string) {
defer wg.Done()
fmt.Println(text)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment