Beego框架使用

时间:2021-07-04 07:43:11

go get github.com/astaxie/beego

vim hello.go

package main

import "github.com/astaxie/beego"

func main() {
    beego.Run()
}

编译运行

go build -o hello hello.go

./hello

Beego框架使用

例子2:

package main

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (this *MainController) Get() {
    this.Ctx.WriteString("hello world")
}

func main() {
    beego.Router("/", &MainController{})
    beego.Run()
}

编译运行

go build -o hello hello.go

./hello

Beego框架使用

例子3:

package main

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

type UserController struct {
    beego.Controller
}

func (this *MainController) Get() {
    this.Ctx.WriteString("hello world")
}

//Get方法
func (this *UserController) Get() {
    this.Ctx.WriteString("hello world222")
}

type IndexController struct {
    beego.Controller
}

//模版使用index.tpl要位于views文件夹下面
func (this *IndexController) Get() {
    this.Data["Website"] = "beego.me"
    this.Data["Email"] = "astaxie@gmail.com"
    this.TplNames = "index.tpl"
}

func main() {
    beego.SetStaticPath("/down1", "download1")
    beego.Router("/", &MainController{})
    beego.Router("/user", &UserController{})
    beego.Router("/index", &IndexController{})
    beego.Run()
}

编译运行

go build -o hello hello3.go

./hello

Beego框架使用

例子3:

// Beego (http://beego.me/)

// @description beego is an open-source, high-performance web framework for the Go programming language.

// @link        http://github.com/astaxie/beego for the canonical source repository

// @license     http://github.com/astaxie/beego/blob/master/LICENSE

// @authors     astaxie

package main

import (
    //相对路径
    "./controllers"
    "github.com/astaxie/beego"
    //从gopath的src目录来
    // "github.com/astaxie/beego/example/beeapi/controllers"
)

//        Objects

//    URL                    HTTP Verb                Functionality
//    /object                POST                    Creating Objects
//    /object/<objectId>    GET                        Retrieving Objects
//    /object/<objectId>    PUT                        Updating Objects
//    /object                GET                        Queries
//    /object/<objectId>    DELETE                    Deleting Objects

func main() {
    beego.RESTRouter("/object2", &controllers.Object2Controller{})
    beego.RESTRouter("/object", &controllers.ObjectController{})
    beego.Run()
}

编译运行

go build -o hello hello3.go

./hello

Beego框架使用

参考:

http://beego.me/

http://beego.me/quickstart

http://studygolang.com/articles/1124

http://www.golangtc.com/t/5461b63a421aa94699000046

http://studygolang.com/articles/1124