在函数中如何处理`http.Get`的错误?

huangapple go评论98阅读模式
英文:

how handle error in http.Get in 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
}

当发生HTTP请求错误时,我得到了panic: runtime error: invalid memory address or nil pointer dereference

我知道我应该检查类似这样的错误:

if err != nil {
    // 在这里处理错误
}

但是我不知道我应该做什么,例如这样:

if err != nil {
    return
}

我得到了./main.go:113: not enough arguments to return

英文:

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

答案1

得分: 2

我不习惯在Go语言中使用HTTP请求,但我猜错误可能来自于*res,因为当出现错误时,res是空的。

我会返回一个指针(*goreq.Response),避免解引用:

func execute(query Query) (*goreq.Response, error) {
    return goreq.Request{
       // 原始代码放在这里
    }.Do()
}

在这种情况下,您将在需要响应值的地方解引用指针,并且如果错误不为空,则可以忽略它。

希望对您有所帮助。

英文:

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.

答案2

得分: 1

最终我按照以下方式解决了这个问题:

if err != nil {
    var empty goreq.Response
    return empty, err
}
英文:

finally i resolve this as follow

if err != nil {
	var empty goreq.Response
	return empty, err
}

答案3

得分: -1

根据我的有限经验,错误也是一种值。你的函数返回两个值,return 是不够的。如果没有要返回的响应,可以使用 return nil, nil。或者你可以使用 panic(err) 来终止程序。这取决于请求失败时你想要做什么。

英文:

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.

huangapple
  • 本文由 发表于 2015年7月7日 22:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/31272189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定