-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (73 loc) · 1.43 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
"sync/atomic"
)
const (
SearchPattern = "Go"
HttpSchemePrefix = "http"
WorkersCount = 5
)
func getHttp(url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
func count(source string) (int, error) {
var (
data []byte
err error
)
if strings.HasPrefix(source, HttpSchemePrefix) {
data, err = getHttp(source)
} else {
data, err = ioutil.ReadFile(source)
}
if err != nil {
return 0, err
}
return strings.Count(string(data), SearchPattern), nil
}
func main() {
var (
semaphore = make(chan struct{}, WorkersCount)
total uint64
wg sync.WaitGroup
scanner = bufio.NewScanner(os.Stdin)
)
for scanner.Scan() {
for _, source := range strings.Split(scanner.Text(), "\n") {
wg.Add(1)
semaphore <- struct{}{}
go func(source string) {
defer func() {
wg.Done()
<-semaphore
}()
count, err := count(source)
if err != nil {
fmt.Fprintf(os.Stderr, "Error for %s: %s\n", source, err)
return
}
fmt.Fprintf(os.Stdout, "Count for %s: %d\n", source, count)
atomic.AddUint64(&total, uint64(count))
}(source)
}
}
if err := scanner.Err(); err != nil {
log.Fatalln("Reading error: ", err)
}
wg.Wait()
close(semaphore)
fmt.Printf("Total: %d\n", total)
}