-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl-checker.go
46 lines (42 loc) · 1.06 KB
/
url-checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"runtime"
"strings"
"sync"
)
func checkAndSaveBody(url string, wg *sync.WaitGroup) {
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
fmt.Printf("%s is Down!\n", url)
} else {
defer resp.Body.Close()
fmt.Printf("%s -> Status Code: %d \n", url, resp.StatusCode)
if resp.StatusCode == 200 {
bodyBytes, err := ioutil.ReadAll(resp.Body)
file := strings.Split(url, "//")[1]
file += ".txt"
fmt.Printf("Writing response body to %s\n", file)
err = ioutil.WriteFile(file, bodyBytes, 0664)
if err != nil {
log.Fatal(err)
}
}
}
wg.Done()
}
func main() {
urls := []string{"https://golang.org", "https://www.google.com", "https://www.medium.com", "https://www.facebook.com", "https://www.instagram.com", "https://www.exapmle.com"}
var wg sync.WaitGroup
wg.Add(len(urls))
for _, url := range urls {
go checkAndSaveBody(url, &wg) // working with goroutines
fmt.Println(strings.Repeat("#", 20))
}
fmt.Println("No. of Goroutines:", runtime.NumGoroutine())
wg.Wait()
}