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

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

beego开发

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

看看本例的结构图

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

package main

@H_301_16@import ( _ "beego_example/router" "github.com/astaxie/beego" ) @H_301_16@func @H_301_16@main() { @H_301_16@beego.R@H_301_16@un() } 

router\router.go

package router

@H_301_16@import ( "beego_example/controller/IndexController" "github.com/astaxie/beego" ) @H_301_16@func @H_301_16@init() { @H_301_16@beego.R@H_301_16@outer("/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

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

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

@H_301_16@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

猜你在找的Go相关文章