使用gorilla mux路由器时如何忽略单词并匹配所有其他单词?

时间:2018-02-26 06:37:00

标签: go mux gorilla

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

1 个答案:

答案 0 :(得分:0)

像这样。

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)
}
相关问题