Golang 导入(未定义:Request)

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

Golang Import (undefined: Request)

问题

我正在尝试创建一个自定义函数,将Request对象作为结果返回:

func ConstructRequest(testParameters string, reqType string) Request {
    req, err := http.NewRequest(reqType, testPath+testParameters, nil)

    if err != nil {
        log.Fatal(err)
        return nil
    } else {
        return req
    }
}

但是我得到了一个"undefined: Request"错误。

我不确定需要导入哪个库才能使其工作?

英文:

I'm trying to create a custom function that passes the Request object back as a result:

 func ConstructRequest(testParameters string, reqType string) Request {
	req, err := http.NewRequest(reqType, testPath+testParameters, nil)

	if err != nil {
		log.Fatal(err)
		return nil
	} else {
		return req
	}
}

but I'm getting a "undefined: Request" error

I'm not sure what library I need to import to make it work?

答案1

得分: 5

undefined: Request如果你没有为返回类型指定包,这种错误是正常的:

func ConstructRequest(testParameters string, reqType string) http.Request
                                                             ^^^^

由于http.NewRequest()返回一个指针,所以应该是:

func ConstructRequest(testParameters string, reqType string) *http.Request
英文:

undefined: Request seems normal if you don't specify the package for the returned type:

func ConstructRequest(testParameters string, reqType string) http.Request
                                                             ^^^^

Since http.NewRequest() returns a pointer, this should be:

func ConstructRequest(testParameters string, reqType string) *http.Request

huangapple
  • 本文由 发表于 2015年5月11日 19:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/30166986.html
匿名

发表评论

匿名网友

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

确定