Undefined - error

814 views
Skip to first unread message

Jai Kumar

unread,
Sep 20, 2012, 1:32:40 PM9/20/12
to golan...@googlegroups.com
package main
import "os"
import "image"

func main() {
    aImg, _ := os.Open("foo.jpg")
    defer aImg.Close()
    decodedImg, _, _ := image.Decode(aImg)
    doSomething(decodedImg)
}

func doSomething(dImg Image) string {
    return "foobar"
}

When I run this it throws error:

prog.go:12: undefined: Image
you can see it at http://play.golang.org/p/wVPMCvg5hD 

What am I doing wrong?

I am sure it must be a stupid question, image.Decode returns type Image, so I should be able to use it?

Jan Mercl

unread,
Sep 20, 2012, 1:39:49 PM9/20/12
to Jai Kumar, golan...@googlegroups.com


On Sep 20, 2012 7:32 PM, "Jai Kumar" <jaiva...@gmail.com> wrote:
>
> What am I doing wrong?

The mighty Gopher kills a kitten whenever an error is ignored.

-j

Adam Gray

unread,
Sep 20, 2012, 1:40:54 PM9/20/12
to Jai Kumar, golan...@googlegroups.com

Jai Kumar

unread,
Sep 20, 2012, 1:44:15 PM9/20/12
to golan...@googlegroups.com, Jai Kumar
You are right, but I have not gotten the award of being "gopher" yet. A newbie trying to learn go.
Changed the code to 

package main
import "os"
import "image"
import "fmt"

func main() {
    aImg, e := os.Open("foo.jpg")
    if e != nil {
     fmt.Println(e)
    }
    defer aImg.Close()
    decodedImg, _, err := image.Decode(aImg)
    if err != nil {
     fmt.Println(err)
    }
    doSomething(decodedImg)
}

func doSomething(dImg Image) string {
    return "foobar"
}


minux

unread,
Sep 20, 2012, 1:46:16 PM9/20/12
to Jai Kumar, golan...@googlegroups.com


On Sep 21, 2012 1:32 AM, "Jai Kumar" <jaiva...@gmail.com> wrote:
>
> package main
> import "os"
> import "image"

you forgot to import _ "image/jpg", or you won't be able to decode jpeg picture.


>
> func main() {
>     aImg, _ := os.Open("foo.jpg")

blindly ignoring the error is the sign of a very bad style (if not the worst)


>     defer aImg.Close()
>     decodedImg, _, _ := image.Decode(aImg)

>     doSomething(decodedImg)
> }
>
> func doSomething(dImg Image) string {

should use image.Image instead of Image

Jai Kumar

unread,
Sep 20, 2012, 1:46:40 PM9/20/12
to golan...@googlegroups.com, Jai Kumar
Per Jan, changed the code again

Jai Kumar

unread,
Sep 20, 2012, 1:49:02 PM9/20/12
to golan...@googlegroups.com, Jai Kumar
Thank you Adam. Sorry for the silly question ;)

Jai Kumar

unread,
Sep 20, 2012, 1:51:54 PM9/20/12
to golan...@googlegroups.com, Jai Kumar
Thanks minux. I am checking the error in the actual code. It was just a test code I put on play.golang and did not paste the whole code. Anyway I appreciate you reminding about error checking and I will continue doing that.

Andrew O'Neill

unread,
Sep 20, 2012, 1:36:39 PM9/20/12
to Jai Kumar, golan...@googlegroups.com
You need to add the package name in front of Image

func doSomething(dImg image.Image) string {

Andrew

On Thu, Sep 20, 2012 at 1:32 PM, Jai Kumar <jaiva...@gmail.com> wrote:

--
 
 

Reply all
Reply to author
Forward
0 new messages