英文:
cannot use err (type error) as type goreq.Error in return argument
问题
我正在尝试从一个方法中返回两个值(result和error),但是我得到了以下错误信息:
无法将err(类型为error)作为返回参数中的goreq.Error类型使用
我的代码如下:
package components
import (
goreq "github.com/franela/goreq"
"time"
)
var UserAgent string = "..."
func Get(url string) (*goreq.Response, goreq.Error) {
goreq.SetConnectTimeout(15 * time.Second)
res, err := goreq.Request{
Uri: url,
UserAgent: UserAgent,
Timeout: 5 * time.Second,
}.Do()
return res, err
}
英文:
I am trying to return two values (result and error) from a method but I get this
cannot use err (type error) as type goreq.Error in return argument
my code
package components
import (
goreq "github.com/franela/goreq"
"time"
)
var UserAgent string = "..."
func Get(url string) (*goreq.Response, goreq.Error) {
goreq.SetConnectTimeout(15 * time.Second)
res, err := goreq.Request{
Uri: url,
UserAgent: UserAgent,
Timeout: 5 * time.Second,
}.Do()
return res, err
}
答案1
得分: 3
Do()
返回类型错误,不是 goreq.Error
。将第二个返回类型从 goreq.Error
改为 error
。
英文:
Do()
returns type error, not goreq.Error
. Change your second return type to error
instead of goreq.Error
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论