beego 的正则路由

* beego的restful路由

https://blog.csdn.net/fareast_mzh/article/details/86667517

* main.go

// beego-route project main.go
package main

import (
"fmt"

"github.com/astaxie/beego"
)

type RegExpController struct {
beego.Controller
}

/**
* http://127.0.0.1:8081/RegExp/123
*/
func (this *RegExpController) Get() {
this.Ctx.WriteString(fmt.Sprintf("<p>In RegExp Mode</p>"))

id := this.Ctx.Input.Param(":id")
this.Ctx.WriteString(fmt.Sprintf("id is %s.<br />", id))

splat := this.Ctx.Input.Param(":splat")
this.Ctx.WriteString(fmt.Sprintf("splat is %s.<br />", splat))

path := this.Ctx.Input.Param(":path")
this.Ctx.WriteString(fmt.Sprintf("path is %s.<br />", path))
ext := this.Ctx.Input.Param(":ext")
this.Ctx.WriteString(fmt.Sprintf("ext is %s.<br />", ext))
}

func main() {

// 正则路由从path中提取参数
beego.Router("/RegExp1/?:id", &RegExpController{})
// 正则表达式匹配数字
beego.Router("/RegExp2/?:id([0-9]+)", &RegExpController{})
beego.Router("/RegExp3/?:id([\\w]+)", &RegExpController{})
// http://127.0.0.1:8081/RegExp4/abc123de (id=123)
beego.Router("/RegExp4/abc:id([0-9]+)de", &RegExpController{})

beego.Router("/RegExp5/*", &RegExpController{})

// http://127.0.0.1:8081/RegExp6/11.txt (path is 11. ext is txt.)
beego.Router("/RegExp6/*.*", &RegExpController{})

// start service
beego.Run("127.0.0.1:8081")
}
* build:

$ go build && ./beego-route

2019/01/27 17:38:40.694 [I]  http server Running on http://127.0.0.1:8081

* test:

http://127.0.0.1:8081/RegExp6/11.txt

 

https://www.imooc.com/video/18637


————————————————
版权声明:本文为CSDN博主「fareast_mzh」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fareast_mzh/java/article/details/86667892

posted on 2020-05-25 16:41  ExplorerMan  阅读(721)  评论(0编辑  收藏  举报

导航