我尝试使用 Golang 编写一个简单的程序,以下是代码:
我尝试使用 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)
}
}
httputil.NewSingleHostReverseProxy
吗? - raina77owhttputil.NewSingleHostReverseProxy
替换了http.NewSingleHostReverseProxy
,错误已经消失了,谢谢。但是http.URL
的错误仍然存在。 - dlionurl.URL
而不是http.URL
。并非所有与 HTTP 相关的函数/类型都集中在net/http
包中。 - raina77ow