新的征程Beego
最初由中国台湾省谢某人编写支持MVC编写模式是Golang中最全面的一个WEB框架借鉴了python-flask及其很多优秀框架的设计模式,内部高度结构提供丰富的内置库使用,是Golang最值得学习的WEB框架之一
- beego官方社区:https://beego.me/docs/install/bee.md
配置Golang-IDE快捷开发
linux初始化Beego
mkdir /beego && cd /beego
#查看当前的GOPATH
go env
#设置环境变量(可以写入~/.bashrc文件中)
export GOROOT=/usr/local/go
#可以设置成自己设计的位置
export GOPATH=/root/go
export GOPROXY="https://goproxy.io,https://goproxy.cn,direct"
export PATH=/usr/local/go/bin:$GOPATH/bin:$PATH
#安装beego工具
go get -u github.com/astaxie/beego
#Bee是管理Beego Web应用程序的快速而灵活的工具。
go get -u github.com/beego/bee
bee new 项目名称
#如果你开启了go mod 模式就需要创建
go mode init 项目名称
bee run #热启动类似django的热更新
IDE环节
- 配置好和linux服务器数据同步使用云开发的乐趣
- 配置好具体信息根据截图
- 设置到这里的时候请在目标机器执行
go get -u github.com/go-delve/delve/cmd/dlv
-
然后去到你的目标
main.go
地方执行dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient
-
就可以畅想云调试了,拒绝win调试的畅爽
目录结构
root@VM-200-89-ubuntu:/beego/src/ChickenBeauty# tree
.
├── ChickenBeauty
├── conf
│ └── app.conf
├── controllers
│ └── default.go
├── main.go
├── models
├── routers
│ └── router.go
├── static
│ ├── css
│ ├── img
│ └── js
│ └── reload.min.js
├── tests
│ └── default_test.go
└── views
├── index.tpl
└── test.html
- conf文件夹:放的是项目有关的配置文件
-
controllers:存放主要的业务代码main.go:项目的入口文件
-
models:存放的是数据库有关内容
-
routers:存放路由文件,路由作用是根据不同的请求指定不同的控制器
-
static:存放静态资源,包括图片,html页面,css样式,js文件等
-
tests:测试文件
-
views:存放视图有关内容
beego设计模式
MVC
- mode1模型操作数据的
-
view 视图控制显示
controller控制器联动的和v的
- 1.浏览器发送请求到控制器
-
2.控制器找模型要数据
-
3.控制器找视图要页面
-
4.把页面返回给浏览器
简单的一个demo实现post方法
-> route.go
func init() {
beego.Router("/", &controllers.MainController{})
//第三个参数控制Url处理的函数的,指定了则优先使用多个方式以;分割
beego.Router("/index",&controllers.IndexController{},"get:ShowGet;post:Post")
}
-----------------------------------------------------------------------------------
-> controller/defaults.go
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.Data["Website"] = "beego.me"
c.Data["Email"] = "astaxie@gmail.com"
c.Data["test"] = "新的一天你好"
c.TplName = "test.html"
}
type IndexController struct {
beego.Controller
}
func (c *IndexController) Post() {
c.Data["test"]="测试通过"
c.TplName= "test.html"
}
func (this *IndexController) ShowGet() {
this.Data["date"]="这个是ShowGet请求的方法"
this.TplName="test.html"
}
-----------------------------------------------------------------------------------
-> view/test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>
nihao {{.date}}
</h1>
<h2>
{{.test}}
</h2>
<form method="post" action="/index">
<input type="submit">
</form>
</body>
</html>
Awesome article post.Thanks Again. Much obliged. bddgegdddadf
Hi there! Would you mind if I share your blog with my myspace group? There’s a lot of people that I think would really appreciate your content. Please let me know. Many thanks ebadkbgkgdbk
You completed a number of fine points there. I did a search on the theme and found the majority of folks will agree with your blog. akefaabkagee