Created
June 15, 2019 00:49
-
-
Save brydavis/e995b627fbfe8f3496d7f18034f35964 to your computer and use it in GitHub Desktop.
Read each file / each line concurrently
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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