Skip to content

Instantly share code, notes, and snippets.

@aerostitch
Created January 3, 2020 22:43
Show Gist options
  • Save aerostitch/ae7874b41076d61e3f4ecfef5761ad3e to your computer and use it in GitHub Desktop.
Save aerostitch/ae7874b41076d61e3f4ecfef5761ad3e to your computer and use it in GitHub Desktop.
Just verifying that the WithContext does not abort the other goroutines in an errgroup
package main
import (
"context"
"fmt"
"math/rand"
"time"
"golang.org/x/sync/errgroup"
)
func main() {
ctx := context.Background()
grp, gCtx := errgroup.WithContext(ctx)
for i := 0; i < 10; i++ {
grp.Go(func() error {
rand.Seed(time.Now().UnixNano())
t := rand.Intn(10)
fmt.Printf("Sleeping %d seconds...\n", t)
time.Sleep(time.Duration(t) * time.Second)
fmt.Println("context err: ", gCtx.Err())
fmt.Printf("Sleeping %d seconds...\n", t)
time.Sleep(time.Duration(t) * time.Second)
fmt.Println("context err: ", gCtx.Err())
return fmt.Errorf("This is an error")
})
}
grp.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment