duanpanyang1962
duanpanyang1962
2016-12-01 04:15

新手编译错误net / http响应

  • http
已采纳

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条回答

  • doubi7739 doubi7739 5年前

    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

    点赞 评论 复制链接分享

为你推荐