您好,我在 go 中使用 gorilla/mux,我想处理任何以“/a/b/c”开头的 url
我试过:
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc(`/a/b/{_dummy:c(\/)?.*}`, Func1)
也就是url可以是/a/b/c/d或者/a/b/c/d/e
最佳答案
根据 gorilla/mux 的文档:http://www.gorillatoolkit.org/pkg/mux#Route.PathPrefix
func (r *Router) PathPrefix(tpl string) *Route
PathPrefix registers a new route with a matcher for the URL path prefix. See Route.PathPrefix().
func (r *Route) PathPrefix(tpl string) *Route
PathPrefix adds a matcher for the URL path prefix. This matches if the given template is a prefix of the full URL path. See Route.Path() for details on the tpl argument.
Note that it does not treat slashes specially ("/foobar/" will be matched by the prefix "/foo") so you may want to use a trailing slash here.
Also note that the setting of Router.StrictSlash() has no effect on routes with a PathPrefix matcher.
Note that the path provided to PathPrefix() represents a "wildcard": calling PathPrefix("/static/").Handler(...) means that the handler will be passed any request that matches "/static/*".
所以你要找的是:
router := mux.NewRouter()
router.PathPrefix("/a/b/c/").HandleFunc(proxy.GrafanaHandler) // matches /a/b/c/*
关于go - 我如何处理以 Go 中的某个 url 开头的任何 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41063435/