gpt4 book ai didi

Gorilla Mux Use() 函数不工作

转载 作者:IT王子 更新时间:2023-10-29 00:43:26 34 4
gpt4 key购买 nike

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

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 包。

非常感谢。

最佳答案

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

关于Gorilla Mux Use() 函数不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49347974/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com