Last active
May 16, 2020 14:59
-
-
Save olehcambel/bfc7e17c56c868a9fd9d7d6c6bbdef85 to your computer and use it in GitHub Desktop.
check status code of websites
This file contains hidden or 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 main | |
import ( | |
"fmt" | |
"net/http" | |
"runtime" | |
) | |
func printDomain(d string) { | |
fmt.Println(d) | |
} | |
func checker(linkCh <-chan string, done chan<- int) { | |
for link := range linkCh { | |
resp, err := http.Get(link) | |
if err != nil { | |
fmt.Println("No response from", link) | |
} else { | |
fmt.Println("Status of %v is", link, resp.StatusCode) | |
} | |
done <- 1 | |
} | |
} | |
func main() { | |
links := []string{ | |
"http://googleeeeeqq.com", | |
"http://facebook.com", | |
"http://stackoverflow.com", | |
"http://golang.org", | |
"http://amazon.com", | |
} | |
nWorkers := runtime.NumCPU() | |
ch := make(chan string, len(links)) | |
done := make(chan int, len(links)) | |
for _, link := range links { | |
ch <- link | |
} | |
for i := 0; i < nWorkers; i++ { | |
go checker(ch, done) | |
} | |
for i := 0; i < len(links); i++ { | |
<-done | |
} | |
fmt.Print("OK") | |
} |
This file contains hidden or 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 main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
) | |
func main() { | |
links := []string{ | |
"http://googleeeeeqq.com", | |
"http://facebook.com", | |
"http://stackoverflow.com", | |
"http://golang.org", | |
"http://amazon.com", | |
} | |
exit := make(chan bool, 1) | |
for _, link := range links { | |
go check(link) | |
} | |
<-exit | |
} | |
func check(link string) { | |
for { | |
resp, err := http.Get(link) | |
if err != nil { | |
log.Println("err with:", link) | |
} else { | |
fmt.Printf("status: %v: %v\n", resp.StatusCode, link) | |
} | |
time.Sleep(time.Second * 2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment