gpt4 book ai didi

http - Golang 错误 : Undefined: http. NewSingleHostReverseProxy

转载 作者:IT王子 更新时间:2023-10-29 01:43:20 27 4
gpt4 key购买 nike

我尝试使用 Golang 构建一个简单的程序,这里是:

package main

import (
"net/http"
"net/http/httputil"
"log"
)

func main(){

proxy := http.NewSingleHostReverseProxy( &http.URL{Scheme:"http",Host:"www.google.com",Path:"/"})

err := http.ListenAndServe(":8080", proxy)
if err != nil {
log.Fatal("ListenAndServe: ", err.String())
}
}

构建:

go build myprogram.go

输出:

command-line-arguments
./myprogram.go:5: imported and not used: "net/http/httputil"
./myprogram.go:11: undefined: http.NewSingleHostReverseProxy
./myprogram.go:11: undefined: http.URL
./myprogram.go:15: err.String undefined (type error has no field or method String)

我注意到 http.NewSingleHostReverseProxy 在“net/http/httputil”包中,为什么我会看到这样的错误?也许我需要一个特定的命令才能正确构建它?

编辑之后,这是新的工作代码:

package main

import (
"net/http"
"net/http/httputil"
"net/url"
"log"
)

func main(){

proxy := httputil.NewSingleHostReverseProxy( &url.URL{Scheme:"http",Host:"www.google.com",Path:"/"})

err := http.ListenAndServe(":8080", proxy)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

最佳答案

我加了

"net/url"

并将 http.NewSingleHostReverseProxy 替换为 httputil.NewSingleHostReverseProxy,同样http.URLurl.URL

这是工作代码:

package main

import (
"net/http"
"net/http/httputil"
"net/url"
"log"
)

func main(){

proxy := httputil.NewSingleHostReverseProxy( &url.URL{Scheme:"http",Host:"www.google.com",Path:"/"})

err := http.ListenAndServe(":8080", proxy)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

感谢 raina77ow 的帮助。

关于http - Golang 错误 : Undefined: http. NewSingleHostReverseProxy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19843335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com