Skip to content

Instantly share code, notes, and snippets.

@pieterclaerhout
Created August 24, 2023 06:51
Show Gist options
  • Save pieterclaerhout/bca25ce2351187f8054faf240794f5dc to your computer and use it in GitHub Desktop.
Save pieterclaerhout/bca25ce2351187f8054faf240794f5dc to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"net/http"
"golang.org/x/sync/errgroup"
)
func main() {
urls := []string{
"https://www.easyjet.com/",
"https://www.skyscanner.de/",
"https://www.ryanair.com",
"https://wizzair.com/",
"https://www.swiss.com/",
}
ctx := context.Background()
g, _ := errgroup.WithContext(ctx)
g.SetLimit(3) // See https://pkg.go.dev/golang.org/x/sync/errgroup#Group.SetLimit
for _, url := range urls {
url := url // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
fmt.Printf("%s: checking\n", url)
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
return nil
})
}
if err := g.Wait(); err != nil {
fmt.Printf("Error: %v", err)
return
}
fmt.Println("Successfully fetched all URLs.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment