-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrgroup-basics-1.go
56 lines (43 loc) · 1.08 KB
/
errgroup-basics-1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"context"
"fmt"
"net/http"
"runtime"
"golang.org/x/sync/errgroup"
)
/*
The main idea of the errgroup pattern is to start a group of goroutines,
wait for them to finish their work, and handle any errors that may occur during execution. */
// change test to main() before the execution
func test() {
urls := []string{
"https://www.easyjet.com/",
"https://ww.sskyscanner.de/",
"https://ww.swiss.com/",
"https://www.ryanair.com",
"https://wizzair.com/",
}
ctx := context.Background()
g, _ := errgroup.WithContext(ctx)
// See https://pkg.go.dev/golang.org/x/sync/errgroup#Group.SetLimit
g.SetLimit(2)
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
})
}
fmt.Println("Number of goroutines", runtime.NumGoroutine())
if err := g.Wait(); err != nil {
fmt.Printf("Error: %v", err)
return
}
fmt.Println("Successfully fetched all URLs.")
}