加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 09:42:18 所属栏目:大数据 来源:网络整理
导读:beego开发 Beego web开发 1、规划好ur 2、添加路由 3、开发controller,继承beego.Controller 看看本例的结构图 mainmain.go 初始化beego,引入router模块 package main import ( _ "beego_example/router" "github.com/astaxie/beego" ) func main () { be

beego开发

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

看看本例的结构图

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

package main

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

routerrouter.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"
}

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

views/index/index.html

<html>
    <body>
        <p> Hello World</p>
    </body></html>

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

PS E:golanggo_prosrcbeego_example> go build beego_example/main
PS E:golanggo_prosrcbeego_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模板使用

待续。。。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读