Propagating errors with errgroup
This recipe will show how to easily use the errgroup extension package to detect the error within the group of goroutines that run subtasks, within a common task.
How to do it...
- Open the console and create the folder
chapter10/recipe07
. - Navigate to the directory.
- Create the file
lines.go
with the following content:
package main import ( "bufio" "context" "fmt" "log" "strings" "golang.org/x/sync/errgroup" ) const data = `line one line two with more words error: This is erroneous line` func main() { log.Printf("Application %s starting.", "Error Detection") scanner := bufio.NewScanner(strings.NewReader(data)) scanner.Split(bufio.ScanLines) // For each line fire a goroutine g, _ := errgroup.WithContext(context.Background()) for scanner.Scan() { row := scanner.Text() ...