golang基础-beego_web开发、模板使用

前端之家收集整理的这篇文章主要介绍了golang基础-beego_web开发、模板使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

beego开发

Beego web开发
1、规划好ur
2、添加路由
3、开发controller,继承beego.Controller

看看本例的结构图

main\main.go
初始化beego,引入router模块

  1. package main
  2.  
  3. import ( _ "beego_example/router" "github.com/astaxie/beego" ) func main() { beego.Run() }

router\router.go

  1. package router
  2.  
  3. 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

  1. package IndexController
  2.  
  3. import (
  4. "github.com/astaxie/beego"
  5. "github.com/astaxie/beego/logs"
  6. )
  7. //继承beego的Controller
  8. type IndexController struct {
  9. beego.Controller
  10. }
  11.  
  12. func (p *IndexController) Index() {
  13.  
  14. logs.Debug("enter index controller")
  15. p.TplName = "index/index.html"
  16. }

p.TplName意思就是加载路径下的html页面文件

views/index/index.html

  1. <html>
  2. <body>
  3. <p> Hello World</p>
  4. </body></html>

接下来进行测试:
由于p.TplName = “index/index.html”我们在beego_example进行编译

  1. PS E:\golang\go_pro\src\beego_example> go build beego_example/main
  2. PS E:\golang\go_pro\src\beego_example> main.exe
  3. 2017/11/26 17:21:21 [I] [asm_amd64.s:2197] http server Running on http://127.0.0.1:9091
  4. 2017/11/26 17:21:38 [D] [asm_amd64.s:514] enter index controller
  5. [beego] 2017/11/26 - 17:21:38 | 127.0.0.1| 200 | 1.5012ms| match| GET /index/ r:/index
  6. [beego] 2017/11/26 - 17:21:38 | 127.0.0.1| 200 | 1.5012ms| match| GET /index/ r:/ind
  7. [beego] 2017/11/26 - 17:21:38 | 127.0.0.1| 200 | 1.5012ms| match| GET /index/ r:/index
  8. [beego] 2017/11/26 - 17:21:38 | 127.0.0.1| 404 | 500.2µs| nomatch| GET /favicon.ico

然后历览器输入:
http://localhost:9091/index/

beego模板使用

待续。。。

猜你在找的Go相关文章