go - 我如何处理以 Go 中的某个 url 开头的任何 url?

标签 go

您好,我在 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/

相关文章:

google-app-engine - 如何从数据存储中获取 ID 以更新实体?

go - 我们如何在 Go 中使用 channel 来代替互斥锁?

git - 为团队构建 Go 子包

go - 无论如何在ansi控制台上打印斜体字体?

go - bytes.Buffer 的限制?

go - 如果有多个节点,Couchbase 操作已超时

go - go-sql-driver Scan() 的命名键

go - 如何使用ws.SetWriteDeadline?

io - 中止来自另一个 goroutine 的 Read() 调用

go - 在这个特定示例中,我应该在哪里关闭 channel ?