From 2b797a5a40f1037965f878e8513cf890f5d52a75 Mon Sep 17 00:00:00 2001 From: Toomore Chiang Date: Sun, 3 Jan 2016 09:51:43 +0000 Subject: [PATCH] Using chan in cmd/lazyflickr_sharetotumblr. --- cmd/lazyflickr_sharetotumblr/main.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/lazyflickr_sharetotumblr/main.go b/cmd/lazyflickr_sharetotumblr/main.go index a7d6e9b..49a7f38 100644 --- a/cmd/lazyflickr_sharetotumblr/main.go +++ b/cmd/lazyflickr_sharetotumblr/main.go @@ -36,20 +36,23 @@ func main() { t.Token = os.Getenv("TUMBLRUSERTOKEN") t.TokenSecret = os.Getenv("TUMBLRUSERSECRET") - num := len(flag.Args()) - photolist := make([]jsonstruct.PhotosGetInfo, num) - wg.Add(num) - for i, photoID := range flag.Args() { - go func(i int, photoID string) { - runtime.Gosched() - defer wg.Done() - photolist[i] = f.PhotosGetInfo(photoID) - }(i, photoID) - } + photolist := make(chan jsonstruct.PhotosGetInfo) + wg.Add(flag.NArg()) + go func() { + for i, photoID := range flag.Args() { + go func(i int, photoID string) { + runtime.Gosched() + photolist <- f.PhotosGetInfo(photoID) + }(i, photoID) + } + }() - wg.Wait() + go func() { + wg.Wait() + close(photolist) + }() - for _, photo := range photolist { + for photo := range photolist { args := make(map[string]string) args["source"] = fmt.Sprintf(imageFormat, photo.Photo.Farm, @@ -83,5 +86,6 @@ func main() { log.Println(warn("[Error:%d] [%s] %s", resp.StatusCode, photo.Photo.ID, photo.Photo.Title.Content)) } + wg.Done() } }