大猩猩Mux没有处理我的路径

时间:2017-04-28 16:14:47

标签: go gorilla

当我使用http中的默认路由器时,一切正常,但如果我使用gorilla/mux中的路由器,我会得到一个带有正文404 page not found的404页面。如下面的示例所示,其他所有内容都完全相同。

为什么gorilla/mux路由器不能像这样工作?

正确使用http路由:

package main

import "net/http"

func simplestPossible(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("MWE says OK"))
}

func main() {
    http.HandleFunc("/", simplestPossible)

    http.ListenAndServe(":8000", nil)
}

无效,使用gorilla/mux路由:

package main

import "net/http"
import "github.com/gorilla/mux"

func simplestPossible(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("MWE says OK"))
}

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", simplestPossible)

    http.ListenAndServe(":8000", nil)
}

1 个答案:

答案 0 :(得分:2)

您必须将处理程序传递给http包(ListenAndServe):

http.ListenAndServe(":8000", r)