Use quit channel.

This commit is contained in:
Fabien Freling 2014-12-16 22:02:32 +01:00
parent 5a19118dae
commit c371fd6aab

12
main.go
View file

@ -9,7 +9,6 @@ import (
"os"
"path"
"strings"
"time"
)
var (
@ -29,8 +28,9 @@ func main() {
defer file.Close()
c := make(chan string, *worker)
quit := make(chan bool)
for i := 0; i < *worker; i++ {
go processFileEntry(c)
go processFileEntry(c, quit)
}
scanner := bufio.NewScanner(file)
@ -39,14 +39,16 @@ func main() {
}
close(c)
// Let's wait 10 sec for the last images to download.
time.Sleep(10 * 1e9)
for i := 0; i < *worker; i++ {
<-quit
}
}
func processFileEntry(c <-chan string) {
func processFileEntry(c <-chan string, quit chan<- bool) {
for str := range c {
downloadFile(str)
}
quit <- true
}
func downloadFile(url string) {