我是靠谱客的博主 欢喜皮卡丘,这篇文章主要介绍Go语言之高级篇beego框架安装与使用,现在分享给大家,希望可以做个参考。

 

一、beego框架

1、beego框架简介

beego 是一个快速开发 Go 应用的 HTTP 框架,他可以用来快速开发 API、Web 及后端服务等各种应用,是一个 RESTful 的框架,主要设计灵感来源于 tornado、sinatra 和 flask 这三个框架,但是结合了 Go 本身的一些特性(interface、struct 嵌入等)而设计的一个框架。

参考官网:https://beego.me/docs/intro/

 

2、beego 的执行逻辑

beego 是一个典型的 MVC 架构,它的执行逻辑如下图所示:

3、beego 项目结构
一般的 beego 项目的目录如下所示:

├── conf
│ └── app.conf
├── controllers
│ ├── admin
│ └── default.go
├── main.go
├── models
│ └── models.go
├── static
│ ├── css
│ ├── ico
│ ├── img
│ └── js
└── views
├── admin
└── index.tpl

从上面的目录结构我们可以看出来 M(models 目录)、V(views 目录)和 C(controllers 目录)的结构, main.go 是入口文件。

 

4、beego框架安装

复制代码
1
go get github.com/astaxie/beego

参考:https://github.com/astaxie/beego

设置GoPATH路径:

 

 

 5、bee工具

复制代码
1
go get github.com/beego/bee

new命令是新建一个Web项目,我们在命令行下执行bee new<项目名>就可以创建一个新的项目。但是注意该命令必段在$GOPATH/src 下执行。

api 命令 是用来创建API应用的。

 

 安装步骤:

#绝对路径,运行:bee

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#安装bee C:Users560140>go get github.com/beego/bee #绝对路径,运行:bee C:Users560140>%GOPATH%/bin/bee Bee is a Fast and Flexible tool for managing your Beego Web Application. USAGE bee command [arguments] AVAILABLE COMMANDS version Prints the current Bee version migrate Runs database migrations api Creates a Beego API application bale Transforms non-Go files to Go source files fix Fixes your application by making it compatible with newer versio ns of Beego dlv Start a debugging session using Delve dockerize Generates a Dockerfile for your Beego application generate Source code generator hprose Creates an RPC application based on Hprose and Beego frameworks new Creates a Beego application pack Compresses a Beego application into a single file rs Run customized scripts run Run the application by starting a local development server server serving static content over HTTP on port Use bee help [command] for more information about a command. ADDITIONAL HELP TOPICS Use bee help [topic] for more information about that topic

# 设置环境变量临时生效

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#设置环境变量,临时生效 C:Users560140>set PATH=%PATH%;%GOPATH%/bin #运行bee C:Users560140>bee Bee is a Fast and Flexible tool for managing your Beego Web Application. USAGE bee command [arguments] AVAILABLE COMMANDS version Prints the current Bee version migrate Runs database migrations api Creates a Beego API application bale Transforms non-Go files to Go source files fix Fixes your application by making it compatible with newer versio ns of Beego dlv Start a debugging session using Delve dockerize Generates a Dockerfile for your Beego application generate Source code generator hprose Creates an RPC application based on Hprose and Beego frameworks new Creates a Beego application pack Compresses a Beego application into a single file rs Run customized scripts run Run the application by starting a local development server server serving static content over HTTP on port Use bee help [command] for more information about a command. ADDITIONAL HELP TOPICS Use bee help [topic] for more information about that topic.

  

 6、beego创建项目

创建web项目: bee new WEB 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
C:Users560140>bee new WEB ______ | ___ | |_/ / ___ ___ | ___ / _ / _ | |_/ /| __/| __/ ____/ ___| ___| v1.10.0 2019/01/29 14:25:14 WARN ▶ 0001 You current workdir is not inside $GOPATH/sr c. 2019/01/29 14:25:14 INFO ▶ 0002 Creating application... create D:GoFilessrcWEB create D:GoFilessrcWEBconf create D:GoFilessrcWEBcontrollers create D:GoFilessrcWEBmodels create D:GoFilessrcWEBrouters create D:GoFilessrcWEBtests create D:GoFilessrcWEBstatic create D:GoFilessrcWEBstaticjs create D:GoFilessrcWEBstaticcss create D:GoFilessrcWEBstaticimg create D:GoFilessrcWEBviews create D:GoFilessrcWEBconfapp.conf create D:GoFilessrcWEBcontrollersdefault.go create D:GoFilessrcWEBviewsindex.tpl create D:GoFilessrcWEBroutersrouter.go create D:GoFilessrcWEBtestsdefault_test.go create D:GoFilessrcWEBmain.go 2019/01/29 14:25:14 SUCCESS ▶ 0003 New application successfully created!

 

创建API项目: bee api API

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
C:Users560140>bee api API ______ | ___ | |_/ / ___ ___ | ___ / _ / _ | |_/ /| __/| __/ ____/ ___| ___| v1.10.0 2019/01/29 14:25:32 WARN ▶ 0001 You current workdir is not inside $GOPATH/sr c. 2019/01/29 14:25:32 INFO ▶ 0002 Creating API... create D:GoFilessrcAPI create D:GoFilessrcAPIconf create D:GoFilessrcAPIcontrollers create D:GoFilessrcAPItests create D:GoFilessrcAPIconfapp.conf create D:GoFilessrcAPImodels create D:GoFilessrcAPIrouters create D:GoFilessrcAPIcontrollersobject.go create D:GoFilessrcAPIcontrollersuser.go create D:GoFilessrcAPItestsdefault_test.go create D:GoFilessrcAPIroutersrouter.go create D:GoFilessrcAPImodelsobject.go create D:GoFilessrcAPImodelsuser.go create D:GoFilessrcAPImain.go 2019/01/29 14:25:33 SUCCESS ▶ 0003 New API successfully created!

  

 7、在LiteIDE中打开WEB项目

把WEB项目导入到LiteIDE中

执行项目:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
D:GoFilessrcWEB>bee run ______ | ___ | |_/ / ___ ___ | ___ / _ / _ | |_/ /| __/| __/ ____/ ___| ___| v1.10.0 2019/01/29 14:33:35 INFO ▶ 0001 Using 'WEB' as 'appname' 2019/01/29 14:33:35 INFO ▶ 0002 Initializing watcher... WEB/controllers WEB/routers WEB 2019/01/29 14:33:41 SUCCESS ▶ 0003 Built Successfully! 2019/01/29 14:33:41 INFO ▶ 0004 Restarting 'WEB.exe'... 2019/01/29 14:33:41 SUCCESS ▶ 0005 './WEB.exe' is running... 2019/01/29 14:33:41.919 [I] [asm_amd64.s:1333] http server Running on http://:8 080

  

 

转载于:https://www.cnblogs.com/nulige/p/10333756.html

最后

以上就是欢喜皮卡丘最近收集整理的关于Go语言之高级篇beego框架安装与使用的全部内容,更多相关Go语言之高级篇beego框架安装与使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部