-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
54 lines (46 loc) · 1.14 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
package main
import (
"encoding/csv"
"flag"
"io"
"io/ioutil"
"log"
"strings"
"github.com/Amalkh5/concurrency-go/pool"
)
func readFromFile(filename string) []string {
var urls []string
//read csv file
file, err := ioutil.ReadFile(filename)
if err != nil {
// log.Fatal is enough
log.Fatal("File reading error", err)
}
r := csv.NewReader(strings.NewReader(string(file)))
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
urls = append(urls, record[0])
}
return urls
}
func main() {
filename := flag.String("filename", "url.csv", "file name of the URLs link")
flag.Parse() //parse flags from command line.
// early return is a cleaner way to write code,
// it avoids nesting and lets the reader knows the errors
// for more info https://blog.timoxley.com/post/47041269194/avoid-else-return-early
if len(*filename) <= 0 {
log.Fatal("filename must be more than zero")
// log.Fatal exits the process so no need to "return", i added it just so you could learn the return-early pattern
return
}
urls := readFromFile(*filename)
p := pool.NewPool()
p.StartTheWorker(urls)
}