learn_go/channels/main.go

41 lines
746 B
Go

package main
import (
"fmt"
"net/http"
"time"
)
func main() {
urls := []string{
"https://google.com",
"https://facebook.com",
"https://golang.org",
"https://johannes-rothe.de",
"https://amazon.com",
}
c := make(chan string)
for _, url := range urls {
go checkURL(url, c)
}
for url := range c { // wait for the channel to return a value, then assign it to l
go func(url string) {
time.Sleep(2 * time.Second)
checkURL(url, c)
}(url)
//go checkURL(url, c)
//fmt.Println(<-c) // value through c is a blocking call
}
}
func checkURL(url string, c chan string) {
_, err := http.Get(url)
if err != nil {
fmt.Printf("%s might be down!\n", url)
c <- url
return
}
fmt.Printf("%s is up!\n", url)
c <- url
}