beego开发
Beego web开发
1、规划好ur
2、添加路由
3、开发controller,继承beego.Controller
看看本例的结构图
main\main.go
初始化beego,引入router模块
package main
import ( _ "beego_example/router" "github.com/astaxie/beego" ) func main() { beego.Run() }
router\router.go
package router
import ( "beego_example/controller/IndexController" "github.com/astaxie/beego" ) func init() { beego.Router("/index",&IndexController.IndexController{},"*:Index") }
Router方法意思就是将url后缀index,交给IndexController下的Index处理
IndexController/index.go
package IndexController
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
)
//继承beego的Controller
type IndexController struct {
beego.Controller
}
func (p *IndexController) Index() {
logs.Debug("enter index controller")
p.TplName = "index/index.html"
}
views/index/index.html
<html>
<body>
<p> Hello World</p>
</body></html>
接下来进行测试:
由于p.TplName = “index/index.html”我们在beego_example进行编译
PS E:\golang\go_pro\src\beego_example> go build beego_example/main
PS E:\golang\go_pro\src\beego_example> main.exe
2017/11/26 17:21:21 [I] [asm_amd64.s:2197] http server Running on http://127.0.0.1:9091
2017/11/26 17:21:38 [D] [asm_amd64.s:514] enter index controller
[beego] 2017/11/26 - 17:21:38 | 127.0.0.1| 200 | 1.5012ms| match| GET /index/ r:/index
[beego] 2017/11/26 - 17:21:38 | 127.0.0.1| 200 | 1.5012ms| match| GET /index/ r:/ind
[beego] 2017/11/26 - 17:21:38 | 127.0.0.1| 200 | 1.5012ms| match| GET /index/ r:/index
[beego] 2017/11/26 - 17:21:38 | 127.0.0.1| 404 | 500.2µs| nomatch| GET /favicon.ico
然后历览器输入:
http://localhost:9091/index/
beego模板使用
待续。。。
原文链接:https://www.f2er.com/go/187626.html