beego 路由简写(值方法分离)

go交流群 852311425  go交流群绝对原创,转载请附上 本微博链接,有帮到你打赏下哦.

基本就是因为懒得重复的的代码 像写下面这种

beego.Router("/", &controllers.MainController{})
beego.Router("/about",&controllers.MainController{},"get:About")

几个问题

1、太长了,编辑器会换行

2、不直观

3、密码麻麻的

修改后要达到的效果,

1、单独编写路由和方法

2、直观

好了,二话不说  ,上代码

package routers

import (
	"github.com/astaxie/beego"
	"hello/controllers"
)

type mapType = map[string]interface{} //
var routerPathArr =[]mapType{
	mapType{
		"path":"/",
	},
	mapType{
		"path":"/about",
		"func":"get:About",
	},mapType{
		"path":"/test",
		"func":"get:Test",
	},
}

func init() {
	routerFun()
}
func routerFun(){
	mainC:=&controllers.MainController{}
	for _,row:=range routerPathArr{
		pathStr:=row["path"].(string)
		if row["func"]!=nil{
			beego.Router(pathStr, mainC,row["func"].(string))
		}else{
			beego.Router(pathStr, mainC)
		}
	}
}

go交流群 852311425  go交流群绝对原创,转载请附上 本微博链接,有帮到你打赏下哦.

猜你喜欢

转载自blog.csdn.net/xuelang532777032/article/details/108703547