I'd like to read lines from a file and process them concurrently. I came up with the following code to do this:
var wg sync.WaitGroup
func main(){
file := "data/input.txt"
reader, _ := os.Open(file)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
wg.Add(1)
go processLine(scanner.Text())
}
wg.Wait()
}
func processLine(line string) {
time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
fmt.Println("line:", line)
wg.Done()
}
I added the random sleep time in there to simulate potential differences in processing times.
Is there any potential drawbacks that I should be aware of with this method of concurrent processing? Are there any better ways that I should consider concurrently processing lines in a file?
processLine
function empty because it's not relevant to the question i'm asking \$\endgroup\$Questions should not contain purely generic, hypothetical code, such as
This is clearly not "...purly generic, hupothetical code". The concurrency code (which is what I'm asking about) is what will be used in my production application \$\endgroup\$