为什么会出现此未定义的类型错误?

时间:2019-07-01 20:34:22

标签: go gorilla mux

这是一个直接的编译错误。我看到在我拥有的类型上定义了Method,所以我不明白为什么它不能编译。

package main

import (
    "github.com/gorilla/mux"
    "fmt"
    "os"
    "net"
    "net/http"
    "net/http/httputil"
    "net/url"
    "time"
    "crypto/tls"
)

func main(){
    router := mux.NewRouter();

    origin, _ := url.Parse("http://localhost:4200/")

    director := func(req *http.Request) {
        req.Header.Add("X-Forwarded-Host", req.Host)
        req.Header.Add("X-Origin-Host", origin.Host)
        req.URL.Scheme = "https"
        req.URL.Host = origin.Host
    }

    proxy := &httputil.ReverseProxy{Director: director}

    proxy.Transport = &http.Transport{
        Proxy: http.ProxyFromEnvironment,
        Dial: (&net.Dialer{
            Timeout:   30 * time.Second,
            KeepAlive: 30 * time.Second,
        }).Dial,
        TLSHandshakeTimeout: 10 * time.Second,
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }

    router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Println("Proxy: "+r.URL.Path)
        proxy.ServeHTTP(w, r)
    })
    router.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Println("Proxy: "+r.URL.Path)
        proxy.ServeHTTP(w, r)
    }))

    router.HeadersRegexp("Content-Type", "application/(text|json)").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Println("Proxy: "+r.URL.Path)
        proxy.ServeHTTP(w, r)
    }))


    err := http.ListenAndServeTLS(":443", "server.crt", "server.key", router)
    if err != nil { fmt.Print(err) }
}

router.HeadersRegexp在gorilla / mux / route.go中定义。有关注释中的示例,请参见第258行。它是从那里复制/粘贴的。可能的原因:router.HeadersRegexp未定义(类型* mux.Router没有字段或方法HeadersRegexp)

谢谢...

0 个答案:

没有答案