Code from https://pkg.go.dev/golang.org/x/sync/errgroup?tab=doc
var g errgroup.Group
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
url := url
g.Go(func() error {
// Fetch the URL.
resp, err := http.Get(url)
if err == nil {
resp.Body.Close()
}
return err
})
}
if err := g.Wait(); err == nil {
fmt.Println("Successfully fetched all URLs.")
}
g.Wait()
.So if your functions handle the context cancelation by exiting, then you can cancel other goroutines.
I asvice using such sub context with requests, too.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7d6b7a91-3af7-48d0-acf9-2bdcdb25e57d%40googlegroups.com.