英文:
newbie compilation error net/http Response
问题
为什么我在这段代码中会得到编译错误?"net/http"中定义了响应类型。
package main
import "net/http"
func main() {
}
func GetWithProxy(urlString string, proxyString string) (resp *Response, err error) {
return nil, nil
}
错误信息:
.\t.go:3: 导入的包未使用: "net/http"
.\t.go:7: 未定义: Response
英文:
Why do I get a compilation error for this code? Response type is defined in "net/http"
package main
import "net/http"
func main() {
}
func GetWithProxy(urlString string, proxyString string) (resp *Response, err error) {
return nil, nil
}
error:
.\t.go:3: imported and not used: "net/http"
.\t.go:7: undefined: Response
答案1
得分: 2
它在抱怨你没有使用net/http
,而你确实没有使用。
package main
import "net/http"
func GetWithProxy(urlString string, proxyString string) (resp *http.Response, err error) {
return nil, nil
}
func main() {
}
现在你使用了net/http
,所以这段代码可以编译通过。编译器不知道你在谈论net/http
的Response
类型。
如果你想要"吸收" net/http
的命名空间,你可以这样做:
package main
import . "net/http"
func GetWithProxy(urlString string, proxyString string) (resp *Response, err error) {
return nil, nil
}
func main() {
}
请注意:
https://play.golang.org/p/WH1NSzFhSV
英文:
It's complaining that you didn't use net/http
, which you didn't.
package main
import "net/http"
func GetWithProxy(urlString string, proxyString string) (resp *http.Response, err error) {
return nil, nil
}
func main() {
}
This will compile because now you are using net/http
. The compiler didn't know you were talking about net/http's
Response
type.
If you want 'absorb' net/http's
namespace you can do:
package main
import . "net/http"
func GetWithProxy(urlString string, proxyString string) (resp *Response, err error) {
return nil, nil
}
func main() {
}
Observe:
https://play.golang.org/p/WH1NSzFhSV
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论