我有一段代码来调整图像大小。我正在使用resize库。这是片段:package mainimport ( "fmt" "github.com/nfnt/resize" "image" "image/jpeg" "os" "runtime")func main() { runtime.GOMAXPROCS(4) file_names := make([]string, 5) for i := 1; i < 6; i++ { file_names[i-1] = fmt.Sprintf("%v", i) } c := make(chan string) ce := make(chan error) for _, filename := range file_names { go func() { proccesFileName(filename, c, ce) }() } for { select { case str := <-c: fmt.Println(str) break case err := <-ce: fmt.Println(err) break } }}这是我的proccesFileName功能:func proccesFileName(filename string, c chan string, ce chan error) { file, err := os.Open(fmt.Sprintf("in/%v.jpg", filename)) if err != nil { ce <- err } defer file.Close() img, err := jpeg.Decode(file) if err != nil { ce <- err } scales := []float32{1.0, 0.8, 0.6, 0.5, 0.25, 0.01} thumbs := make([]image.Image, len(scales)) for i := 0; i < len(scales); i++ { thumbs[i] = resize.Resize(uint(float32(img.Bounds().Max.X)*scales[i]), uint(float32(img.Bounds().Max.Y)*scales[i]), img, resize.Lanczos3) thumb_name := fmt.Sprintf("out/%v_thumb_%v.jpg", filename, i+1) out, err := os.Create(thumb_name) if err != nil { ce <- err } defer out.Close() jpeg.Encode(out, thumbs[i], nil) c <- fmt.Sprintf("%v FINISHED", i) }}我有输出:0 FINISHED0 FINISHED0 FINISHED0 FINISHED0 FINISHED1 FINISHED1 FINISHED1 FINISHED2 FINISHED1 FINISHED1 FINISHED2 FINISHED3 FINISHED2 FINISHED2 FINISHED2 FINISHED3 FINISHED4 FINISHED4 FINISHED3 FINISHED5 FINISHED3 FINISHED5 FINISHED3 FINISHED4 FINISHED4 FINISHED4 FINISHED5 FINISHED5 FINISHED5 FINISHED问题可能出在哪里?
1 回答
拉莫斯之舞
TA贡献1820条经验 获得超10个赞
您无需无限期地等待。您知道消息将如何在频道上播放。
for _ = range file_names {
select {
case str := <-c
fmt.Println(str)
case err := <-ce
fmt.Println(err)
}
}
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报
0/150
提交
取消