I have a CSV file with thousands of records. I process each record in a goroutine and I want to gather all the results of processed records in a slice to write them down in another file. Simplified version of my code is:
var wg sync.WaitGroup
func main() {
c := make(chan string, 1)
csvfile, err := os.Open("data.csv")
reader := csv.NewReader(bufio.NewReader(csvfile))
//...
for {
line, err := reader.Read()
// ...
wg.Add(1)
go processRecord(line, c)
}
//...
results := make([]string, 0)
for r := range c { // ERROR HERE: fatal error: all goroutines are asleep - deadlock!
results = append(results , r)
}
// write results to file
wg.Wait()
close(c)
}
func processRecord(data []string, c chan string) {
defer wg.Done()
// ...
c <- *result
}
In for loop I get all the results but loop continues after getting last result and I get this error:
fatal error: all goroutines are asleep - deadlock!
What is wrong with this code? I tried different QAs on SO without any success.
CodePudding user response:
The for-loop will only terminate once the channel is closed. You are closing the channel after for-loop terminates, hence the deadlock.
You can fix by putting the for-loop into a goroutine:
results := make([]string, 0)
done:=make(chan struct{})
go func() {
for r := range c {
results = append(results , r)
}
close(done)
}()
wg.Wait()
close(c)
<-done
// read results