O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter10/recipe07.
  2. Navigate to the directory.
  3. 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()            g.Go(func() error {              return func(s string) error {                if strings.Contains(s, "error:") {                  return fmt.Errorf(s)                }                return nil              }(row)            })          } // ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required