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!

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵