beego2---入门

简介:
复制代码
package main

//beego既可以建立web应用也可以建立api应用

//beego是一个gohttp服务器的高层封装,

import (
    _ "beego1/routers"
    "github.com/astaxie/beego"
)

//beego是通过controller和router进行路由的转发
//控制器
type HomeController struct {
    beego.Controller //嵌入一个控制器,默认就有一些方法,
    //例如post方法,在没有自定义post方法的时候不会报错,
}

//控制器的get方法
func (this *HomeController) Get() { //给结构体添加方法,那么浏览器发起请求的时候
    //就会执行这个get方法,重写了本身controller的方法,
    this.Ctx.WriteString("hello")

}

func main() {
    //配置路由
    beego.Router("/", &HomeController{}) //用户访问根目录的时候,就会调用HomeController
    //当用get方法访问的时候就调用get方法,
    beego.Run()
}
复制代码

 

复制代码
package main

import (
    //_ "beego1/routers"   //默认controll文件夹里面的控制器
    "github.com/astaxie/beego"
    //"strconv"
)

type HomeController struct {
    beego.Controller
}

func (this *HomeController) Get() {
    this.Ctx.WriteString("appname::::::" + beego.AppConfig.String("appname") +
        "\nhttpport" + beego.AppConfig.String("httpport") +
        "\nrunmode:" + beego.AppConfig.String("runmode")) //读取的是conf里面的app.conf文件里面的内容

    // hp := strconv.Itoa(beego.HttpPort)
    // this.Ctx.WriteString("appname:" + beego.AppName +
    //     "\nhttpport" + hp +
    //     "\nrunmode:" + beego.RunMode) //读取的是conf里面的app.conf文件里面的内容

    //打印
    beego.Trace("trace")
    beego.Info("info")
    beego.Debug("debug")
    beego.Warn("warn")
    beego.Error("error")

}

func main() {
    beego.Router("/", &HomeController{})
    beego.Run()
}
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/8086815.html,如需转载请自行联系原作者

相关文章
|
7月前
|
缓存 机器人 Go
Golang 语言 beego 学习之安装和快速入门
Golang 语言 beego 学习之安装和快速入门
37 0
|
10月前
|
缓存 NoSQL 中间件
用golang搭建springboot风格项目结构 gin+gorm
最近学了学go语言,想练习一下用go开发web项目,项目结构弄个什么样呢。
|
7月前
|
缓存 机器人 Go
Golang 语言 beego v2 之安装和快速入门
Golang 语言 beego v2 之安装和快速入门
173 0
|
11月前
|
JSON Linux Go
Golang之我想写个"web框架"-7: 完成一个“留言小demo”
Golang之我想写个"web框架"-7: 完成一个“留言小demo”
136 0
|
前端开发
随笔---Beego中的routers包个人的理解
随笔---Beego中的routers包个人的理解
随笔---Beego中的routers包个人的理解
|
XML JSON Java
|
存储 中间件 Go
|
XML JSON Java
|
Go API 开发工具
beego的安装和简单使用
beego 安装 升级 bee bee工具的安装 使用 bee bee new bee api bee run bee pack bee version
505 0
运行beego项目时报错:cannot find package “github.com/astaxie/beego“ in any of
运行beego项目时报错:cannot find package “github.com/astaxie/beego“ in any of
480 0
运行beego项目时报错:cannot find package “github.com/astaxie/beego“ in any of