Created
January 23, 2019 05:26
-
-
Save huahuayu/317abd14ac2062a163d58c098fdef002 to your computer and use it in GitHub Desktop.
[unbuffered channel]使用unbuffered channel goroutine轮询网站 #goroutine #channel #go
This file contains 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 unbuffered | |
import ( | |
"fmt" | |
"net/http" | |
"time" | |
) | |
func main() { | |
links := []string{ | |
"http://baidu.com", | |
"http://qq.com", | |
"http://taobao.com", | |
"http://jd.com", | |
"http://z.cn", | |
} | |
c := make(chan string) | |
for _, link := range links { | |
go func(link string) { | |
checkLink(link, c) | |
}(link) | |
} | |
for l := range c { | |
go func(link string) { | |
time.Sleep(5 * time.Second) | |
checkLink(link, c) | |
}(l) | |
} | |
} | |
func checkLink(link string, c chan string) { | |
_, err := http.Get(link) | |
defer func() { c <- link }() | |
if err != nil { | |
fmt.Println(link, "might be down!") | |
} else { | |
fmt.Println(link, "is up!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment