http - 新手编译报错net/http响应

标签 http go httpresponse

为什么我会收到此代码的编译错误?响应类型在“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 的 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

关于http - 新手编译报错net/http响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40902533/

相关文章:

java - 将 java 示例从 GET 转换为 POST

转到 "unknown token"和 ": or newline expected"

Go:如何遍历文件并将 ModTime 与日期进行比较?

sms - Twilio SMS 验证目标电话号码和验证 ID

javascript - Angular 1.6 $http.get 无法读取 json

android - 平台不允许 Flutter Insecure http

variables - Golang 中不同的变量类型声明

json - 为什么我从这个 http 请求中得到空响应

c# - 响应.Cookies : How to append with expiration?

python - 从 python cgi 检测 http 请求类型(GET、HEAD 等)