Use quit channel.

master
Fabien Freling 2014-12-16 22:02:32 +01:00
parent 5a19118dae
commit c371fd6aab
1 changed files with 7 additions and 5 deletions

12
main.go
View File

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