【问题标题】:How to ignore a word and match all other words when using gorilla mux router?使用 gorilla mux 路由器时如何忽略一个单词并匹配所有其他单词?
【发布时间】:2018-02-26 14:55:46
【问题描述】:

例如,我有一个处理“/items/{item-id}”的函数和另一个处理“/items/request-task”的函数。如何使第一个 func 忽略“/items/request-task”并匹配其余部分?

【问题讨论】:

  • mux 允许您仅按顺序控制处理程序的优先级。如果你有几个合适的模式,最早定义的处理程序将被调用。

标签: go mux gorilla


【解决方案1】:

像这样。

package main

import (
    "net/http"

    "github.com/gorilla/mux"
)

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/items/request-task", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("task."))
    }) // task HandleFunc before other
    r.HandleFunc("/items/{item-id}", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("other."))
    })
    http.Handle("/", r)
    http.ListenAndServe(":8080", nil)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多