Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gorilla Mux子例程方法请求后触发GET_Go_Gorilla - Fatal编程技术网

Gorilla Mux子例程方法请求后触发GET

Gorilla Mux子例程方法请求后触发GET,go,gorilla,Go,Gorilla,我正在尝试使用Gorilla Mux在Go中开发一个简单的RESTAPI 我有main.go,它注册上面的简单路径并启动服务器在端口3000上侦听 func main() { router := mux.NewRouter().StrictSlash(true) sub := router.PathPrefix("/api/v1").Subrouter() handlers.RegisterRoutes(sub) log.Fatal(http.ListenAnd

我正在尝试使用Gorilla Mux在Go中开发一个简单的RESTAPI

我有main.go,它注册上面的简单路径并启动服务器在端口3000上侦听

func main() {
    router := mux.NewRouter().StrictSlash(true)
    sub := router.PathPrefix("/api/v1").Subrouter()
    handlers.RegisterRoutes(sub)

    log.Fatal(http.ListenAndServe(":3000", router))
}
另一个通用处理程序.go文件中的基本处理程序注册方法

func RegisterRoutes(sub *mux.Router) {
    user.RegisterRoutes(sub)
}
以及注册“/user”子例程的user.handler.go文件:

func RegisterRoutes(sub *mux.Router) {
    userRoutes := sub.StrictSlash(true).Path("/users").Subrouter()

    userRoutes.Methods("POST").HandlerFunc(getUsers)
    userRoutes.Methods("GET").HandlerFunc(getUsers)
}

func getUsers(w http.ResponseWriter, r *http.Request) {
    user := User{Name: "test", Password: "test"}

    fmt.Printf("%+v\n", r.Method)

    json.NewEncoder(w).Encode(user)
}
我正在测试我在上面设置的路径,并想出了一个古怪的行为:

Test - GET - localhost:3000/api/v1/users => Prints GET in console. (as expected) Test - GET - localhost:3000/api/v1/users/ => Prints GET in console. (as expected) Test - POST - localhost:3000/api/v1/users => Prints POST in console. (as expected) Test - POST - localhost:3000/api/v1/users/ => Prints GET in console. (And here is the strange behavior) Test-GET-localhost:3000/api/v1/users=>在控制台中打印GET。(如预期) Test-GET-localhost:3000/api/v1/users/=>在控制台中打印GET。(如预期) Test-POST-localhost:3000/api/v1/users=>在控制台中打印POST。(如预期) Test-POST-localhost:3000/api/v1/users/=>进入控制台。(这里是奇怪的行为) 当我向端点(localhost:3000/api/users/)发送一篇文章时,在url的末尾有一个尾随斜杠,它会触发GET而不是POST


有人在使用Gorilla Mux时遇到过这种行为吗?

具体问题是,它仍然悬而未决(即使已关闭),也可以在中看到

这似乎也与:StrictSlash令人困惑

这个

如果为true,则如果路由路径为“/path/”,则访问“/path”将重定向到前者,反之亦然。换句话说,应用程序将始终看到路由中指定的路径

如果为false,则如果路由路径为“/path”,则访问“/path/”将与此路由不匹配,反之亦然

应倒过来,因为
strict==true
应表示不允许使用尾随斜杠。
它的名字和文档很混乱