-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
65 lines (59 loc) · 1.19 KB
/
url.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bufio"
"bytes"
"fmt"
"net/http"
"os"
"sync"
)
type Answer struct {
Url string
Count int
}
func main() {
answers := make(chan Answer)
end := make(chan struct{})
go func(answers <-chan Answer, end chan<- struct{}) {
var total int
for answer := range answers {
fmt.Printf("Count for %s: %d\n", answer.Url, answer.Count)
total += answer.Count
}
fmt.Printf("Total: %d\n", total)
end <- struct{}{}
}(answers, end)
var wg sync.WaitGroup
k := make(chan struct{}, 5)
for _, url := range os.Args[1:] {
k <- struct{}{}
wg.Add(1)
go func(url string, answers chan<- Answer, k <-chan struct{}, wg *sync.WaitGroup) {
answers <- Get(url)
<-k
wg.Done()
}(url, answers, k, &wg)
}
wg.Wait()
close(k)
close(answers)
<-end
}
func Get(url string) Answer {
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
return Answer{}
}
scan := bufio.NewScanner(resp.Body)
var count int
for bts := scan.Scan(); bts != false; bts = scan.Scan() {
c := bytes.Count(scan.Bytes(), []byte("Go"))
count = count + c
}
result := Answer{
Url: url,
Count: count,
}
return result
}