beego安装
beego安装是典型的Go安装包的形式
go get github.com/astaxie/beego 复制代码
Beego升级
方式一
go get -u github.com/astaxie/beego 复制代码
方式二
go install github.com/astaxie/beego 复制代码
bee工具的使用
bee工具安装
go get github.com/beego/bee 复制代码
bee工具命令
GOPATH
bee # 查看所有命令 bee new myproject # 新疆web项目 bee api apiapplication # 创建api应用 bee run # 编译运行 go get myproject # bee run error情况下执行该指令(编译项目) 复制代码
beego项目结果
myproject ├── conf │ └── app.conf ├── controllers │ └── default.go ├── main.go ├── models ├── routers │ └── router.go ├── static │ ├── css │ ├── img │ └── js ├── tests │ └── default_test.go └── views └── index.tpl 8 directories, 4 files 复制代码
新建项目
创建项目
bee new beego_pro1 复制代码
运行项目
bee run 或 go run main.go 或者 go build main.go 编译后执行 beego_pro1.exe文件 复制代码
路由设置
先来看一下main.go
package main import ( _ "beego_pro1/routers" "github.com/astaxie/beego" ) func main() { beego.Run() } 复制代码
main_ "beego_pro1/routers"
routers/router.go
/controlleruser/userUserController
beego.Router("/user", &controllers.UserController{}) 复制代码
package routers import ( "beego_pro1/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) } 复制代码
controller运行机制
controllers/default.go
package controllers import ( "fmt" "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (c *MainController) Get() { c.Data["Website"] = "beego.me" c.Data["Email"] = "astaxie@gmail.com" c.TplName = "index.tpl" } 复制代码
MainControllerbeego.ControllerGoMainControllerbeego.Controller
beego.ControllerInitPreparePostGetDeleteHead
Model逻辑
bee newModelbee apimodel
package models import ( "errors" "strconv" "time" ) var ( Objects map[string]*Object ) type Object struct { ObjectId string Score int64 PlayerName string } func init() { Objects = make(map[string]*Object) Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"} Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"} } func AddOne(object Object) (ObjectId string) { object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10) Objects[object.ObjectId] = &object return object.ObjectId } func GetOne(ObjectId string) (object *Object, err error) { if v, ok := Objects[ObjectId]; ok { return v, nil } return nil, errors.New("ObjectId Not Exist") } func GetAll() map[string]*Object { return Objects } func Update(ObjectId string, Score int64) (err error) { if v, ok := Objects[ObjectId]; ok { v.Score = Score return nil } return errors.New("ObjectId Not Exist") } func Delete(ObjectId string) { delete(Objects, ObjectId) } 复制代码
View编写
ControllerGETthis.TplName = "index.tpl"tplhtmlbeego.AddTemplatExt
package routers import ( "beego_pro1/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) ... beego.Router("/index", &controllers.Index{}) } 复制代码
controllers/default.go
package controllers import ( "fmt" "github.com/astaxie/beego" ) type Index struct { beego.Controller } func (c *Index) Get() { // 传参 c.Data["name"] = "阿九筒的春天" c.Data["email"] = "Joe@gmail.com" c.TplName = "index.html" } 复制代码
views/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div> <span>第一个go页面</span><br><br> <span>{{.name}}</span><br><br> <span>{{.email}}</span><br><br> </div> </body> </html> 复制代码
静态文件的导入
bee new projectstatic
├── static │ ├── css │ ├── img │ └── js 复制代码
down123.xlsbeego.Run()
beego.SetStaticPath("/static", "static") // 静态文件 beego.SetStaticPath("/down", "down") // 文件下载 复制代码
https://localhost:8080/down/123.xlsdown123.xls
... // 导入static/css文件夹下的index.css文件 <link href="/static/css/index.css" rel="stylesheet"> <div> <span>第一个go页面</span><br><br> <span>{{.name}}</span><br><br> <span>{{.email}}</span><br><br> <a href="/down/123.xls"><button>点击下载《123.xls》</button></a> </div> ... 复制代码
static/css/index.css
div { font-size: 20px; color: brown; margin: 20px; padding: 10px; } 复制代码
routers/router.go
func init() { beego.Router("/", &controllers.MainController{}) ... beego.Router("/index", &controllers.Index{}) } 复制代码
controllers/default.go
type Index struct { beego.Controller } func (c *Index) Get() { // 传参 c.Data["name"] = "阿九筒的春天" c.Data["email"] = "Joe@gmail.com" c.TplName = "index.html" } 复制代码
controllerc.Data["key"]{{.key}}
几个好玩的路由
router
package routers import ( "beego_pro1/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.MainController{}) beego.Router("/hello", &controllers.HelloWorld{}) beego.Router("/params", &controllers.ParamsTest{}) beego.Router("/index", &controllers.Index{}) } 复制代码
- controller 复制代码
package controllers import ( "fmt" "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (c *MainController) Get() { c.Data["Website"] = "beego.me" c.Data["Email"] = "astaxie@gmail.com" c.TplName = "index.tpl" } type HelloWorld struct { beego.Controller } func (c *HelloWorld) Get() { // hello world c.Ctx.WriteString("hello world") } type ParamsTest struct { beego.Controller } func (c *ParamsTest) Get() { // 获取params参数 var b string = c.Ctx.Request.RequestURI fmt.Println(b) c.Ctx.WriteString(b) } type Index struct { beego.Controller } func (c *Index) Get() { // 传参 c.Data["name"] = "阿九筒的春天" c.Data["email"] = "Joe@gmail.com" c.TplName = "index.html" } 复制代码
控制台399次请求,并返回params
for (var i=1;i<400;i++){ console.log(i);window.location.href="http://localhost:8080/params?i="+String(i);}; 复制代码