英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论