golang web开发框架 Beego

x33g5p2x  于2022-06-27 转载在 其他  
字(1.6k)|赞(0)|评价(0)|浏览(194)

1 介绍

Beego是一个开源的基于Golang的MVC框架,主要用于Golang Web开发。Beego可以用来快速开发API、Web、后端服务等各种应用。

个人开发 api类:gin
团队大项目:Beego

Github:https://github.com/astaxie/beego

官网:https://beego.vip/

2 安装 运行

下载安装
https://github.com/beego/beego/

2.1 安装bee脚手架

go get github.com/beego/bee

输入:bee 查看是否成功

2.2 创建项目

bee new beegodemo01

2.3 使用mod管理项目

go.mod

go mod init

2.4 运行项目

bee run

报错:controllers\default.go:4:2: missing go.sum entry for module providing package git hub.com/astaxie/beego (imported by beegodemo01); to add:

go mod tidy

然后重新运行项目

2.5 目录

├─.idea
│  └─inspectionProfiles
├─conf
├─controllers
├─models
├─routers
├─static
│  ├─css
│  ├─img
│  └─js
├─tests
└─views

3 Beego框架

3.1 MVC

  • Mode(模型):主要用于处理应用程序的业务逻辑,以及和数据库打交道
  • View(视图):是应用程序中数据显示的部分
  • Controller(控制器):作用于模型和视图上。它可以把我们在Model模型上面获取的数据显示到View视图上面,也可以吧view传递的数据流向模型对象。

3.2 Beego中的控制器

beego中控制器本质上是一个结构体,这个结构体里面内嵌了 beego.Controller,继承以后自动拥有了所有 beego.Controller的属性方法。

controller

package controllers

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

type ArticleController struct {
	beego.Controller
}

// http://127.0.0.1:8080/article
func (c *ArticleController) Get() {
	c.Ctx.WriteString("文章") // 直接返回数据
}

// http://127.0.0.1:8080/article/add
func (c *ArticleController) AddArticle() {
	c.Ctx.WriteString("增加新闻")
}

// http://127.0.0.1:8080/article/edit
func (c *ArticleController) EditArticle() {
	c.Ctx.WriteString("修改新闻")
}

router

// http://127.0.0.1:8080/article
beego.Router("/article", &controllers.ArticleController{})
// http://127.0.0.1:8080/article/add
beego.Router("/article/add", &controllers.ArticleController{}, "get:AddArticle")
// http://127.0.0.1:8080/article/edit
beego.Router("/article/edit", &controllers.ArticleController{}, "get:EditArticle")

3.3 GET

相关文章