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("

In RegExp Mode

")) id := this.Ctx.Input.Param(":id") this.Ctx.WriteString(fmt.Sprintf("id is %s.
", id)) splat := this.Ctx.Input.Param(":splat") this.Ctx.WriteString(fmt.Sprintf("splat is %s.
", splat)) path := this.Ctx.Input.Param(":path") this.Ctx.WriteString(fmt.Sprintf("path is %s.
", path)) ext := this.Ctx.Input.Param(":ext") this.Ctx.WriteString(fmt.Sprintf("ext is %s.
", 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

你可能感兴趣的:(golang,go,web,route,regexp)