【问题标题】:Gorilla Mux not handling my pathGorilla Mux 没有处理我的路径
【发布时间】:2017-04-29 00:26:45
【问题描述】:

当我使用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)
}

【问题讨论】:

    标签: go gorilla


    【解决方案1】:

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

    http.ListenAndServe(":8000", r)
    

    【讨论】:

    • 叹息。我正在关注的复数视力课程没有提及任何相关内容,并且提供的示例文件也没有提及。但是现在你展示它,它似乎很明显:)
    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 2021-01-04
    • 2014-09-08
    • 2015-04-05
    • 2015-04-07
    • 2014-11-28
    • 2015-04-02
    • 2021-05-04
    相关资源
    最近更新 更多