Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use errors.New("something wrong") (type error) as type error in return argument

Tags:

go

i have the following code (http://play.golang.org/p/47rvtGqGFn). it works on playground but fail on my system

 package main

import (
   "log"
   "errors" 
)

func main() {
    j := &JustForTest{}
    a, err := j.Test(3)
    if err != nil {
        log.Println(err)
    }
    log.Println(a)
}

type JustForTest struct {}

func (j *JustForTest) Test(i int) (string, error) {
    if i < 5 {
        return "fail", errors.New("something wrong")
    }
    return "success", nil
}

in playground it return something i expected :

2009/11/10 23:00:00 something wrong
2009/11/10 23:00:00 fail

But when I run "go install" on my system, I get compiler error :

../warehouse/warehouse.go:32: cannot convert nil to type error
../warehouse/warehouse.go:83: cannot use errors.New("something wrong") (type error) as type error in return argument
../warehouse/warehouse.go:85: cannot use nil as type error in return argument

I feel this is really weird. I have upgrade my Go installation from 1.3.2 to 1.3.3 but still i get the same error. What could possibly wrong with my system?

UPDATE: Here's my local code :

package warehouse

import (
    "net/http"
    "path"
    "log"
    "errors"
)


func Route(w http.ResponseWriter, r *http.Request) {


    uri := path.Dir(r.URL.Path)

    base := path.Base(r.URL.Path)
    if uri == "/" || (uri == "/products" && base != "products") {
        uri = path.Join(uri, base)
    }

    // initiate all types
    warehouse := Warehouse{}
    inventory := Inventory{}

    // check the request method
    if r.Method == "GET" {
        switch uri {
        case "/warehouse":
            j := &JustForTest{}     // This is the troubled code
            a, err := j.Test(3)
            if err != nil {
                log.Println(err)
            }
            log.Println(a)
            ...
       }
} 

type JustForTest struct {}

func (j *JustForTest) Test(i int) (string, error) {
    if i < 5 {
        return "fail", errors.New("something wrong")
    }
    return "success", nil
}

I found that when i run this code, I get the error. But when I write a new fresh package and just write the exactly the same code as i did in the plaground. it works. I don't understand why this is happening. any advice guys?

like image 700
under5hell Avatar asked Sep 30 '22 13:09

under5hell


2 Answers

It seems like you have defined a type called error in your package.

That's causing a name-collision with the built-in error type (your package-defined type shadows the built-in type) and hence the confusing error message you're seeing.

Use a different, more descriptive (less-general) name for your package's type.

like image 150
thwd Avatar answered Oct 11 '22 20:10

thwd


In addition to @tomwilde response, I would recommend using fmt.Errorf() function to build errors. You would also need to add import of "fmt" package and could remove import of "errors" package.

Your code could be changed from:

if i < 5 {
    return "fail", errors.New("something wrong")
}

To:

if i < 5 {
    return "fail", fmt.Errorf("something wrong")
}

Advantage of using fmt.Errorf() approach is that you can also provide formatting to build your error message, which might be very handy in the future e.g. fmt.Errorf("some error .... %#v", aValue)

I hope that will help.

like image 2
Tom Avatar answered Oct 11 '22 20:10

Tom