新手编译错误 net/http 回应

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: imported and not used: "net/http"
.\t.go:7: undefined: Response

抱怨你没用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's Response 类型。

如果你想要 'absorb' net/http's 命名空间,你可以这样做:

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