Skip to content

Instantly share code, notes, and snippets.

@zhaomengit
Created March 29, 2017 06:42
Show Gist options
  • Save zhaomengit/3bd51220c63f5c4541d697348b86d4f7 to your computer and use it in GitHub Desktop.
Save zhaomengit/3bd51220c63f5c4541d697348b86d4f7 to your computer and use it in GitHub Desktop.
// makeThumbnails6 makes thumbnails for each file received from the channel.
// It returns the number of bytes occupied by the files it creates.
func makeThumbnails6(filenames <-chan string) int64 {
sizes := make(chan int64)
var wg sync.WaitGroup // number of working goroutines
for f := range filenames {
wg.Add(1)
// worker
go func(f string) {
defer wg.Done()
thumb, err := thumbnail.ImageFile(f)
if err != nil {
log.Println(err)
return
}
info, _ := os.Stat(thumb) // OK to ignore error
sizes <- info.Size()
}(f)
}
// closer
go func() {
wg.Wait()
close(sizes)
}()
var total int64
for size := range sizes {
total += size
}
return total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment