I have a problem whith this function:
func execute(query Query) (goreq.Response, error) {
res, err := goreq.Request{
Uri: "http://path/to/host",
QueryString: query,
Accept: "application/json",
UserAgent: "XXXGoClient/1.0",
Timeout: 2000 * time.Millisecond,
Compression: goreq.Gzip(),
//ShowDebug: true,
}.Do()
return *res, err
}
i am getting panic: runtime error: invalid memory address or nil pointer dereference
when a error in http request ocurrs.
I know that i would check for something like this:
if err != nil {
// do somthing here
}
but i dont know what i must to do, for example this:
if err != nil {
return
}
i get ./main.go:113: not enough arguments to return
I'm not used to http requests in go, but I guess the error comes from '*res' as 'res' is nil when there's an error.
What I'd do is to return a pointer (*goreq.Response), avoiding the dereference:
func execute(query Query) (*goreq.Response, error) {
return goreq.Request{
// original code goes here
}.Do()
}
In that case you will dereference the pointer where the value of the response is needed and can be ignored if the error is not nil.
Hope it helps.
From my limited experience, errors are values. Your function returns two values, return
, ain't enough. How about return nil, nil
if there is no response to return anyway... Or you can bring it down with panic(err)
. Depends on what you want to do if the request fails.
finally i resolve this as follow
if err != nil {
var empty goreq.Response
return empty, err
}