Gorilla Mux 不处理我的路径
Gorilla Mux not handling my path
当我使用来自 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)
}
您必须将处理程序传递给 http 包 (ListenAndServe):
http.ListenAndServe(":8000", r)
当我使用来自 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)
}
您必须将处理程序传递给 http 包 (ListenAndServe):
http.ListenAndServe(":8000", r)