I load an image in golang to the image.Image format but i get this error:
"image: unknown format"
This is my function:
import "image"
url := "https://i.imgur.com/m1UIjW1.jpg"
func loadImageFromURL(URL string) (image.Image, error) {
//Get the response bytes from the url
response, err := http.Get(URL)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, errors.New("received non 200 response code")
}
img, _, err := image.Decode(response.Body)
if err != nil {
return nil, err
}
return img, nil
}
What is was missing was this import:
import _ "image/jpeg"