Created
August 24, 2023 06:51
-
-
Save pieterclaerhout/bca25ce2351187f8054faf240794f5dc to your computer and use it in GitHub Desktop.
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" | |
"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