final version of error handling for iterator

This commit is contained in:
Jian Han 2018-05-02 21:53:08 +10:00
parent e15ab34ed7
commit 617693de02

View File

@ -5,6 +5,8 @@ import (
"net/http"
"time"
"sync"
"github.com/davecgh/go-spew/spew"
)
@ -77,7 +79,11 @@ func v2() {
results := make(chan Result)
go func() {
defer close(results)
wg := sync.WaitGroup{}
wg.Add(len(urls))
for _, url := range urls {
go func(url string) {
defer wg.Done()
select {
case <-done:
return
@ -86,20 +92,21 @@ func v2() {
resp, err := http.Get(url)
results <- Result{Error: err, Response: resp}
}
}(url)
}
wg.Wait()
}()
return results
}
done := make(chan interface{})
done := make(chan interface{}, 1)
defer close(done)
urls := []string{
"https://www.google.com",
"https://blog.golang.org/pipelines",
"https://github.com/jianhan",
"https://insights.stackoverflow.com/survey/2017",
"A",
"https://hackernoon.com/top-10-python-web-frameworks-to-learn-in-2018-b2ebab969d1a",
"B",
"https://blog.kowalczyk.info/article/1Bkr/3-ways-to-iterate-in-go.html",
}
for result := range checkStatus(done, urls...) {
if result.Error != nil {