栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

http.HandleFunc模式中的通配符

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

http.HandleFunc模式中的通配符

http.Handler和http.HandleFunc的模式不是正则表达式或glob。无法指定通配符。它们记录在这里。

也就是说,创建自己的可以使用正则表达式或所需的任何其他模式的处理程序并不难。这是一个使用正则表达式(已编译,但未经测试)的表达式:

type route struct {    pattern *regexp.Regexp    handler http.Handler}type RegexpHandler struct {    routes []*route}func (h *RegexpHandler) Handler(pattern *regexp.Regexp, handler http.Handler) {    h.routes = append(h.routes, &route{pattern, handler})}func (h *RegexpHandler) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request)) {    h.routes = append(h.routes, &route{pattern, http.HandlerFunc(handler)})}func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {    for _, route := range h.routes {        if route.pattern.MatchString(r.URL.Path) { route.handler.ServeHTTP(w, r) return        }    }    // no pattern matched; send 404 response    http.NotFound(w, r)}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436219.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号