英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论