FAQ
I cannot find the problem. Please help, thanks.


package main

import (
     "fmt"
     "image"
     "log"
     "os"
     "path/filepath"
     "runtime"
     "strings"
)

var workers = runtime.NumCPU()

type tagInfo struct {
     height int
     width int
     name string
}

func main() {
     if len(os.Args) == 1 {
         log.Fatal("Usage: %s <imagename1 [imagename2] ...>",
filepath.Base(os.Args[0]))
     }

     for _, str := range os.Args[1:] {
         ext := strings.ToLower(filepath.Ext(str))

         if ext != ".png" && ext != ".jpg" && ext != ".jpeg" {
             log.Fatal("Usage: %s <imagename1 [imagename2] ...>",
filepath.Base(os.Args[0]))
         }
     }

     images := make(chan string, workers*4)
     results := make(chan tagInfo, 4)

     procressImgTag(images, results, os.Args[1:])
     showResult(results, len(os.Args[1:]))
}

func procressImgTag(img chan string, result chan tagInfo, imgname []string)
{
     for i := 0; i != workers; i++ {
         go doJob(img, result)
     }

     for _, str := range imgname {
         img <- str
     }
     close(img)
}

func doJob(img <-chan string, result chan<- tagInfo) {

     for filename := range img {
         if info, err := os.Stat(filename); err != nil ||
             (info.Mode()&os.ModeType == 1) {
             log.Print("Failed to check file ", filename, ". Error is ", err)
             return
         }
         file, err := os.Open(filename)
         if err != nil {
             log.Print("Failed to open file ", filename, ". Error is ", err)
             return
         }
         defer file.Close()
         config, _, err := image.DecodeConfig(file)
         if err != nil {
             log.Print("Failed to decode file ", filename, ". Error is ",
err)
             return
         }

         result <- tagInfo{width: config.Width, height: config.Height, name:
filename}
     }
}

func showResult(result chan tagInfo, total int) {
     for i := 0; i != total; i++ {
         rlt := <-result
         fmt.Println(rlt)
     }
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Martin Schnabel at May 5, 2013 at 2:56 pm

    On 05/05/2013 03:42 PM, Leopold Freeman wrote:
    I cannot find the problem. Please help, thanks. ...
    func doJob(img <-chan string, result chan<- tagInfo) {

    for filename := range img {
    if info, err := os.Stat(filename); err != nil ||
    (info.Mode()&os.ModeType == 1) {
    log.Print("Failed to check file ", filename, ". Error is ",
    err)
    return
    }
    file, err := os.Open(filename)
    if err != nil {
    log.Print("Failed to open file ", filename, ". Error is ", err)
    return
    }
    defer file.Close()
    config, _, err := image.DecodeConfig(file)
    if err != nil {
    log.Print("Failed to decode file ", filename, ". Error is
    ", err)
    return
    }

    result <- tagInfo{width: config.Width, height: config.Height,
    name: filename}
    }
    }
    do not just return if you expect a fixed number of results and encounter
    an error.
    instead add an error field to tagInfo and send it to the result channel.
    you will not see the error in your program because log.Print does not
    flush the line buffer. add an "\n" or use log.Println instead.

    you can try it with this simplified version:
        http://play.golang.org/p/0bkoInEysP/p/0bkoInEysP

    hope that helps

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Leopold Freeman at May 6, 2013 at 1:32 am

    I got it. I didn't put the following codes in import section. And
    image.DecodeConfig(file) failed. Then I returned. Of course it is a
    deadlock. because showResult will never return.
         _ "image/gif"
         _ "image/jpeg"
         _ "image/png"

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 5, '13 at 2:29p
activeMay 6, '13 at 1:32a
posts3
users2
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase