Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image.Decode doesn't work as expect #27728

Closed
MaeThird opened this issue Sep 18, 2018 · 2 comments
Closed

image.Decode doesn't work as expect #27728

MaeThird opened this issue Sep 18, 2018 · 2 comments

Comments

@MaeThird
Copy link

I'm testing image.Decode with a truly PNG file with these pieces of code:

logo,err := ioutil.ReadFile(os.Args[1]) 
if err != nil{
	fmt.Println(err)
}
_ ,_ , err = image.Decode(bytes.NewReader(logo))
if err != nil {
	fmt.Println(err)
}
_,err = png.Decode(bytes.NewReader(logo))
if err != nil{
	fmt.Println(err)
}

with _,err = png.Decode(bytes.NewReader(logo)) appended the image.Decode works well,but when I commented out it like this :

logo,err := ioutil.ReadFile(os.Args[1]) 
if err != nil{
	fmt.Println(err)
}
_ ,_ , err = image.Decode(bytes.NewReader(logo))
if err != nil {
	fmt.Println(err)
}

it errors and output is "image: unknown format"
Why should this be happened ? Should I register all the decoder by hand so image.Decode can work correctly ?

@bcmills
Copy link
Contributor

bcmills commented Sep 18, 2018

See https://tip.golang.org/pkg/image/:

Registration is typically automatic as a side effect of initializing that format's package so that, to decode a PNG image, it suffices to have

import _ "image/png"

in a program's main package. The _ means to import a package purely for its initialization side effects.

@bcmills bcmills closed this as completed Sep 18, 2018
@MaeThird
Copy link
Author

@bcmills Thanks.

@golang golang locked and limited conversation to collaborators Sep 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants