// 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
}
func makeThumbnail(filenames []string) (thumbfiles []string, err error) {

	type item struct {
		thumbnail string
		err       error
	}

	ch := make(chan item, len(filenames))

	for _, f := range filenames {
		go func(f string) {
			var it item
			it.thumbnail, it.err = thumbnail.ImageFile(f)
		}(f)
	}

	for range filenames {
		it := <-ch
		if it.err != nil {
			return nil, it.err
		}

		thumbfiles = append(thumbfiles, it.thumbnail)
	}

	return thumbfiles, nil
}
Exemple #3
0
func main() {
	//fmt.Println(time.Unix(1389058332, 0).Format("2006-01-02 15:04:05"))
	//fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
	go spinner(100 * time.Millisecond)
	_, err := thumbnail.ImageFile("")
	if err != nil {
		fmt.Println(err)
	}
	const n = 45
	fibN := fib(45)
	fmt.Println("\rFibonacci(%d) = %d\n", n, fibN)
}