Golang错误:未定义:http.NewSingleHostReverseProxy

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

Golang Error: Undefined: http.NewSingleHostReverseProxy

问题

我尝试使用Golang构建一个简单的程序,代码如下:

  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. "log"
  6. )
  7. func main() {
  8. proxy := http.NewSingleHostReverseProxy(&http.URL{Scheme: "http", Host: "www.google.com", Path: "/"})
  9. err := http.ListenAndServe(":8080", proxy)
  10. if err != nil {
  11. log.Fatal("ListenAndServe: ", err.Error())
  12. }
  13. }

构建:

  1. go build myprogram.go

输出:

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

我注意到http.NewSingleHostReverseProxynet/http/httputil包中,为什么会出现这样的错误呢?也许我需要使用特定的命令来正确构建它?

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

  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. "net/url"
  6. "log"
  7. )
  8. func main() {
  9. proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: "www.google.com", Path: "/"})
  10. err := http.ListenAndServe(":8080", proxy)
  11. if err != nil {
  12. log.Fatal("ListenAndServe: ", err)
  13. }
  14. }

希望对你有帮助!

英文:

I tried building a simple program using Golang, here it is:

  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. "log"
  6. )
  7. func main(){
  8. proxy := http.NewSingleHostReverseProxy( &http.URL{Scheme:"http",Host:"www.google.com",Path:"/"})
  9. err := http.ListenAndServe(":8080", proxy)
  10. if err != nil {
  11. log.Fatal("ListenAndServe: ", err.String())
  12. }
  13. }

Build:

  1. go build myprogram.go

Output:

  1. command-line-arguments
  2. ./myprogram.go:5: imported and not used: "net/http/httputil"
  3. ./myprogram.go:11: undefined: http.NewSingleHostReverseProxy
  4. ./myprogram.go:11: undefined: http.URL
  5. ./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:

  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. "net/url"
  6. "log"
  7. )
  8. func main(){
  9. proxy := httputil.NewSingleHostReverseProxy( &url.URL{Scheme:"http",Host:"www.google.com",Path:"/"})
  10. err := http.ListenAndServe(":8080", proxy)
  11. if err != nil {
  12. log.Fatal("ListenAndServe: ", err)
  13. }
  14. }

答案1

得分: 3

我添加了"net/url",并将http.NewSingleHostReverseProxy替换为httputil.NewSingleHostReverseProxy,还将http.URL替换为url.URL

这是可工作的代码:

  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. "net/url"
  6. "log"
  7. )
  8. func main() {
  9. proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: "www.google.com", Path: "/"})
  10. err := http.ListenAndServe(":8080", proxy)
  11. if err != nil {
  12. log.Fatal("ListenAndServe: ", err)
  13. }
  14. }

感谢raina77ow的帮助。

英文:

I added

  1. "net/url"

And replaced http.NewSingleHostReverseProxy with httputil.NewSingleHostReverseProxy, also
http.URL with url.URL.

This is the working code:

  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httputil"
  5. "net/url"
  6. "log"
  7. )
  8. func main(){
  9. proxy := httputil.NewSingleHostReverseProxy( &url.URL{Scheme:"http",Host:"www.google.com",Path:"/"})
  10. err := http.ListenAndServe(":8080", proxy)
  11. if err != nil {
  12. log.Fatal("ListenAndServe: ", err)
  13. }
  14. }

Thanks to raina77ow for help.

huangapple
  • 本文由 发表于 2013年11月8日 02:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/19843335.html
匿名

发表评论

匿名网友

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

确定