I'm new to Go so I apologize in advance if the answer to my question is obvious :)
I'm planning a producer that reads a file and send each line to a channel, like:
scanner := bufio.NewScanner(file)
for scanner.Scan() {
processingChan <- scanner.Text()
}
and add some goroutines to consume the lines.
now, what I want is that if ANY line fails to process in a goroutine (let's say the line contains an invalid value for my business rules), I want to stop the producer loop, close the file (already defered) and finish the program.
the question is: how can I "notify" the producer loop/for to stop?
I found someone suggesting:
for scanner.Scan() {
select {
case <- quit:
// break / return
default:
// send next line to channel
}
}
and the consumer goroutines would write to a "quit" (or error) channel in case of any fault.
this approach possibly solves the question, but I wonder if there is a cleaner/better or just common/popular approach.