beego学习笔记(3)

相对复杂一点的示例:

package main

import "github.com/astaxie/beego"

type MainController struct{
    beego.Controller
}

func(this *MainController) Get(){
    this.Ctx.WriteString("Hello World!")
}

func main() {
    beego.Router("/",&MainController{})
    beego.Run()
}

说明如下:

1)导入BEEGO的包,GO会按照深度优先的顺序,来进行一些初始化。因此,通过这个导入包,就可以进行BEEGO的一些初始化。

2)MainController实际上是利用了GO的匿名组合的特点。实际上,可以粗浅的理解为,继承了beego.Controller这个struct(类似类)

3)重写restful方法。实际上,通过继承,我们的Maincontroller已经具有了诸如get,post等方法。这里重写了post方法。

4 )注册路由

5)运行。

猜你喜欢

转载自www.cnblogs.com/aomi/p/9249622.html