【问题标题】:Gorilla Mux Use() Function Not WorkingGorilla Mux Use() 函数不工作
【发布时间】:2018-08-27 03:04:35
【问题描述】:

我想使用 Gorilla Mux 包的 Use() 函数,但我无法让它工作。它说:r.Use undefined (type *mux.Router has no field or method Use)。我使用了文档中的相同示例。我的代码如下所示。

package main

import (
    "net/http"
    "github.com/gorilla/mux"
    "fmt"
)

func simpleMw(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Println(r.RequestURI)
        next.ServeHTTP(w, r)
    })
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "hello")
}

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", handler)
    r.Use(simpleMw)
    http.Handle("/", r)
    http.ListenAndServe(":8000", nil)
}

您可以在此处找到文档示例:http://www.gorillatoolkit.org/pkg/mux#overview,搜索“中间件”。

我知道我可以使用this 方法,但我想使用 Gorilla 包。

非常感谢。

【问题讨论】:

  • 为什么不用 negroni 来处理中间件
  • 这段代码对我有用。你用的是什么版本的大猩猩?
  • 哈!你说的对。应该是很老的了。我用go get -u 更新了我的 Gorilla 包,现在它可以工作了。非常感谢!!

标签: go gorilla mux


【解决方案1】:

感谢 Ivan Velichko,我解决了我的问题。我的包裹已经过时了。我用go get -u github.com/gorilla/mux 更新了它,现在它可以工作了。非常感谢你们!

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 2014-12-22
    • 2017-06-19
    • 2014-09-26
    • 1970-01-01
    • 2021-06-05
    • 2017-06-14
    • 2016-05-06
    • 2015-05-11
    相关资源
    最近更新 更多