新手编译错误 net/http 响应

huangapple go评论74阅读模式
英文:

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/httpResponse类型。

如果你想要"吸收" 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

huangapple
  • 本文由 发表于 2016年12月1日 12:15:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/40902533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定