Created
January 3, 2020 22:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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