英文:
Golang Error: Undefined: http.NewSingleHostReverseProxy
问题
我尝试使用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.Error())
}
}
构建:
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)
}
}
希望对你有帮助!
英文:
I tried building a simple program using Golang, here it is:
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())
}
}
Build:
go build myprogram.go
Output:
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)
I noticed that http.NewSingleHostReverseProxy is in the "net/http/httputil" package, so why did I see such errors?
Maybe I need a specific command to build it correctly?
EDIT
Afterwords, here is the new working code:
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)
}
}
答案1
得分: 3
我添加了"net/url"
,并将http.NewSingleHostReverseProxy
替换为httputil.NewSingleHostReverseProxy
,还将http.URL
替换为url.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的帮助。
英文:
I added
"net/url"
And replaced http.NewSingleHostReverseProxy
with httputil.NewSingleHostReverseProxy
, also
http.URL
with url.URL
.
This is the working code:
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)
}
}
Thanks to raina77ow for help.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论