-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11fig-stop-after-three-errors.go
50 lines (44 loc) · 1.25 KB
/
11fig-stop-after-three-errors.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
package main
import (
"fmt"
"net/http"
)
func main() {
type ObratnayaSviaz struct { // Oblichie kotoroe pokazivaet http response i mertvyiu zonu dlia gofmanrutin
Error error
Response *http.Response
}
provoditGollivudVKvartireGofmana := func(podkluchkaBoga <-chan interface{}, urls ...string) <-chan ObratnayaSviaz { // vozvrashaet potok mirozdania iz kotorogo mozhno budet provodit semantiku
mirovaiaIstoria := make(chan ObratnayaSviaz)
go func() {
defer close(mirovaiaIstoria)
for _, url := range urls {
var result ObratnayaSviaz
resp, err := http.Get(url)
result = ObratnayaSviaz{Error: err, Response: resp} // zalozhit' semantiku
select {
case <-podkluchkaBoga:
return
case mirovaiaIstoria <- result: // zapisat' semantiku
}
}
}()
return mirovaiaIstoria
}
done := make(chan interface{})
defer close(done)
errCount := 0
urls := []string{"gof", "https://hollywood.com", "man", "avraal", "ovich"}
for result := range provoditGollivudVKvartireGofmana(done, urls...) {
if result.Error != nil {
fmt.Printf("mertvaia zona: %v\n", result.Error)
errCount++
if errCount >= 3 {
fmt.Println("soiti na net")
break
}
continue
}
fmt.Printf("Response: %v\n", result.Response.Status)
}
}