-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpChannel.go
50 lines (46 loc) · 1.21 KB
/
httpChannel.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
47
48
49
50
package main
import (
"fmt"
"io/ioutil"
"net/http"
"runtime"
"strings"
)
func checkAndSaveBody1(url string, c chan string) {
resp, err := http.Get(url)
if err != nil {
//fmt.Println(err)
s := fmt.Sprintf("%s is Down!\n", url)
s += fmt.Sprintf("Error: %v\n", err)
c <- s
} else {
defer resp.Body.Close()
s := fmt.Sprintf("%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"
s += fmt.Sprintf("Writing response body to %s\n", file)
err = ioutil.WriteFile(file, bodyBytes, 0664)
if err != nil {
//log.Fatal(err)
s += "Error writing file\n"
c <- s
}
s += fmt.Sprintf("%s is Up\n", url)
c <- s
}
}
}
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"}
c := make(chan string)
for _, url := range urls {
go checkAndSaveBody1(url, c) // working with goroutines
fmt.Println(strings.Repeat("#", 20))
}
fmt.Println("No. of goroutines:", runtime.NumGoroutine())
for i := 0; i < len(urls); i++ {
fmt.Println(<-c)
}
}