-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (38 loc) · 876 Bytes
/
main.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
)
func checkAndSaveBody(url string, wg *sync.WaitGroup) {
if resp, err := http.Get(url); 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", url)
err = ioutil.WriteFile(file, bodyBytes, 0664)
if err != nil {
log.Fatal(err)
}
}
}
wg.Done()
}
func main() {
urlNames := []string{"https://www.google.com", "https://www.tesla.com", "https://www.apple.com"}
var wg sync.WaitGroup
wg.Add(len(urlNames))
for _, url := range urlNames {
go checkAndSaveBody(url, &wg)
}
wg.Wait()
}