无法在GOLANG中解码不同的图像格式(分配计数不匹配)

huangapple go评论80阅读模式
英文:

Not able to decode different image formats in GOLANG (assignment count mismatch)

问题

我想解码不同格式的图像,但在调用image.Decode时遇到了问题。

这是我的代码和我看到的错误:

/myproject.go:21: 赋值计数不匹配: 2 = 3

package main

import (
 "fmt"
 "image"
 "log"
 "os"
)

func check(e error) {
 if e != nil {
	 log.Fatal(e)
 }
}

func loadanddeconde(filename string) image.Image {
  reader, err := os.Open(filename)
  check(err)
  defer reader.Close()

/*21行*/  img, err := image.Decode(reader)
  check(err)
  return img
  }

func main() {
 img := loadanddeconde("/home/gomok/Bureau/JSK.jpg")
 fmt.Printf("图像类型: %T \n", img)
}
英文:

I want to decode images of different formats and I'm running into problems calling image.Decode.

Here's my code and here's the error I'm seeing:

/myproject.go:21: assignment count mismatch: 2 = 3

package main

import (
 "fmt"
 "image"
 "log"
 "os"
)

func check(e error) {
 if e != nil {
	 log.Fatal(e)
 }
}

func loadanddeconde(filename string) image.Image {
  reader, err := os.Open(filename)
  check(err)
  defer reader.Close()

/*21.line*/  img, err := image.Decode(reader)
  check(err)
  return img
  }

func main() {
 img := loadanddeconde("/home/gomok/Bureau/JSK.jpg")
 fmt.Printf("IMAGE TYPE: %T \n", img)
}

答案1

得分: 3

这只是Go编译器在告诉你从image.Decode返回了3个值,而你只给了其中2个赋值。

请按照以下方式修改代码:

img, format, err := image.Decode(reader)
fmt.Printf("图片类型:%s\n", format)
英文:

So that's just the go compiler saying you're getting 3 values returned from image.Decode and you're only assigning 2 of them.

Do this:

img, format, err := image.Decode(reader)
fmt.Printf("Image Type: %s\n", format)

huangapple
  • 本文由 发表于 2017年3月1日 10:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/42521927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定