package main import ( "fmt" "net/http" ) type Results struct { Error error Response *http.Response } func main() { checkStatus := func(done <-chan interface{}, urls ...string) <-chan Results { results := make(chan Results) go func() { defer close(results) for _, url := range urls { resp, err := http.Get(url) result := Results{err, resp} select { case <-done: return case results <- result: } } }() return results } urls := []string{"http://www.google.com", "http://www.bing.com", "http://www.amazon.com", "http://qq.com", "http://facebook.com"} done := make(chan interface{}) defer close(done) errorCount := 0 for res := range checkStatus(done, urls...) { if res.Error != nil { errorCount++ if errorCount >= 2 { fmt.Println("too many errors,breaking!") break } fmt.Printf("error:%v\n", res.Error) continue } fmt.Printf("response status:%v\n", res.Response.Status) } }
【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://www.cnblogs.com/facetwitter/p/18293856
标签:res,fmt,并发,博主,go,错误处理,Response From: https://www.cnblogs.com/facetwitter/p/18293856