The purpose of this program in to decode a img embedded with "embed".
The image (bu.png
) is in the same directory of the main.go.
- It seem that the embed way fail to set the exact same resources
package main
import (
"bytes"
_ "embed"
"image"
)
var (
//go:embed bu.png
img []byte
)
func main() {
a := bytes.NewBuffer(img)
a, b, e := image.Decode()
println(e.Error())
// image: unknown format
println(b)
//
println(a)
// (0x0,0x0)
// println(string(img))
// the text of the image seem a little different between nano
}
the image data shold be in the img variabile cause "embed" import
CodePudding user response:
This isn't an embed
thing. You have to import the individual libraries you want to support. Their initialization will register their formats for use by image.Decode
. To quote the aforelinked,
Decoding any particular image format requires the prior registration of a decoder function.
Try adding an import like,
_ "image/png"
I tested this with the following, which should convince you that embed
is irrelevant:
package main
import (
_ "embed"
"fmt"
"bytes"
"image"
//_ "image/png"
//_ "image/jpeg"
//_ "image/gif"
"os"
)
var (
//go:embed bu.png
img []byte
)
func main() {
f, err := os.Open("bu.png")
if err != nil {
panic(fmt.Errorf("Couldn't open file: %w", err))
}
defer f.Close()
fmt.Println(image.Decode(f))
buf := bytes.NewBuffer(img)
fmt.Println(image.Decode(buf))
}