From c371fd6aabf763d32b60e9552ae68e188cf67df3 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Tue, 16 Dec 2014 22:02:32 +0100 Subject: [PATCH] Use quit channel. --- main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 6e99f82..28caf60 100644 --- a/main.go +++ b/main.go @@ -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) {