Skip to content

Instantly share code, notes, and snippets.

@gillesdemey
Last active September 11, 2020 14:32
Show Gist options
  • Save gillesdemey/bdd9584bb944cc6dacbd156c77dd856c to your computer and use it in GitHub Desktop.
Save gillesdemey/bdd9584bb944cc6dacbd156c77dd856c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"golang.org/x/sync/errgroup"
"time"
)
func main() {
fmt.Println("Hello, world!")
eg := new(errgroup.Group)
eg.Go(asyncOne)
eg.Go(asyncTwo)
if err := eg.Wait(); err != nil {
fmt.Println("Encountered error:", err)
}
fmt.Println("Bye bye")
}
func asyncOne() error {
time.Sleep(3 * time.Second)
fmt.Println("async one")
return fmt.Errorf("Error happened")
}
func asyncTwo() error {
time.Sleep(1 * time.Second)
fmt.Println("async two")
return nil
}
@gillesdemey
Copy link
Author

❯ go run main.go
Hello, world!
async two
async one
Encountered error: Error happened
Bye bye

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment