dougao9864 2016-12-09 15:14
浏览 26
已采纳

如何在Go中以某个网址开头的任何网址进行处理?

Hi I am using gorilla/mux in go, and I want to handle any url that begins with : "/a/b/c"

I tried:

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc(`/a/b/{_dummy:c(\/)?.*}`, Func1)

that is the url can be /a/b/c/d or /a/b/c/d/e

  • 写回答

1条回答 默认 最新

  • doucang2871 2016-12-09 16:15
    关注

    Per the documentation for 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/*".

    So what you're looking for is:

    router := mux.NewRouter()
    router.PathPrefix("/a/b/c/").HandleFunc(proxy.GrafanaHandler) // matches /a/b/c/*
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog