duanfu6160 2018-03-18 12:39
浏览 106
已采纳

大猩猩Mux Use()函数不起作用

I want to use the Use() function of the Gorilla Mux package, but I cannot get it to work. It says: r.Use undefined (type *mux.Router has no field or method Use). I used almot the identitcal example from the documentation. My code looks like this.

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)
}

You can find the example of the documentation here: http://www.gorillatoolkit.org/pkg/mux#overview, search for "middleware".

I know I could use this method, but I would like to use the Gorilla package.

Many thanks.

  • 写回答

1条回答 默认 最新

  • duanju9104 2018-03-18 12:54
    关注

    Thanks to Ivan Velichko, I solved my problem. My package was outdated. I updated it with go get -u github.com/gorilla/mux and now it’s working. Many thanks to y’all!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数