Created
April 6, 2021 17:40
-
-
Save rusco/0fcb54e3b467445ae622255a82acf032 to your computer and use it in GitHub Desktop.
Merge 2 csv files line by line in Go via goroutines
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
// go build -ldflags "-s -w" .\mergecsv.go | |
// analyse 2 csv files line by line sequentially via goroutines | |
// 04/2021 | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"math/rand" | |
"os" | |
"time" | |
) | |
func file(name string) *os.File { | |
file, err := os.Open(name) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return file | |
} | |
func fn1(c chan string, fileName string) { | |
file := file(fileName) | |
defer file.Close() | |
defer close(c) | |
scanner1 := bufio.NewScanner(file) | |
for scanner1.Scan() { | |
var msec = rand.Intn(1) | |
time.Sleep(time.Duration(msec) * time.Millisecond) | |
c <- scanner1.Text() | |
} | |
} | |
func main() { | |
rand.Seed(time.Now().Unix()) | |
c1, c2 := make(chan string), make(chan string) | |
go fn1(c1, "file1.txt") | |
go fn1(c2, "file2.txt") | |
for { | |
msg1, ok1 := <-c1 | |
msg2, ok2 := <-c2 | |
if !ok1 && !ok2 { | |
break | |
} | |
fmt.Printf("one: %s\n", msg1) | |
fmt.Printf("two: %s\n", msg2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment