Golang错误:未定义:http.NewSingleHostReverseProxy

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

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.NewSingleHostReverseProxynet/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.

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:

确定