GO 语言的web框架很多,相对来讲, Beego
框架,入门轻便,文书档案齐全(汉语),功效庞大,本文以Beego 示例.

Beego介绍

  beego作者感到是go初学者相比较易于上手的一门MVC
Web框架。轻易易懂,最注重的一点就是提供了华语文书档案,那对于自身这种立陶宛(Lithuania)语技艺非常差的人来讲正是福音。

  beego的官互联网是那般介绍beego的:“beego 是叁个高效支付 Go 应用的 HTTP
框架,他能够用来连忙支付 API、Web 及后端服务等各样应用,是贰个 RESTful
的框架,主要设计灵感来源于 tornado、sinatra 和 flask
那多个框架,不过结合了 Go 自身的片段风味(interface、struct
嵌入等)而设计的一个框架”。从当中能够看见beego能够急忙支付API接口、能够达成服务端的种种应用

beego简介

Beego是七个异常快支付Go应用的http框架,可用于飞快支付Api、web及后端服务等种种应用,是二个RESTful框架。

Beego提供了详尽的支出文书档案:

Beego官网

  beego官网 :

  github : 

beego的架构

Beego由多个单身模块塑造,是二个可观解耦的使用,客商能够不是用beego的http逻辑使用个中的放肆模块。具体架构划设想计如下:

图片 1

1.安装Beego和Bee工具

beego 的安装是规范的 Go 安装包的形式:

终端运行:go get github.com/astaxie/beego,安装在$GOPATH/src目录中

图片 2

bee 工具是一个为了帮扶快速支付 beego
项目而成立的体系,您能够经过 bee
急忙成立项目、达成热编写翻译、开辟测验以及支付完之后打包中丞布的一站式从创建、开辟到布署的方案。

 终端运行:go get github.com/beego/bee,安装完之后,bee可实施文件暗许存放在$GOPATH/bin里头,所以要求把$GOPATH/bin加上到您的情状变量中,才方可扩充下一步。

图片 3

 2.新建三个 Web 项目

执行 bee new ``myproject,但是注意该命令必须在 $GOPATH/src 下试行。最终会在 $GOPATH/src 相应目录下转移如下目录结构的种类:

myproject
├── conf
│   └── app.conf
├── controllers
│   └── default.go
├── main.go
├── models
├── routers
│   └── router.go
├── static
│   ├── css
│   ├── img
│   └── js
├── tests
│   └── default_test.go
└── views
    └── index.tpl

Beego安装

  beego 包涵部分演示应用程序以帮你学习并动用 beego 应用框架。

  您需求设置 Go 1.1+ 以确定保障全数功用的不奇怪化使用。

  你须求设置 Beego 和 Bee 的开垦工具:

$ go get github.com/astaxie/beego
$ go get github.com/beego/bee

  注意:

  1. beege和bee是四个概念。beego是框架,bee是工具,是命令。
  2. 在设置Beego前,先认但是不是将$GOPATH/bin写入GO景况中。

beego推行逻辑

beego是二个美丽的mvc框架,它的施行逻辑如下:

图片 4

与之相应的花色目录结构如下:

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

咱俩得以观望mian.go是进口文件,controllers是逻辑调控模块,models是模型模块,views是视图模块。

3.周转品种

开采LiteIDE,加载项目目录,运营:

图片 5

网址已运转,大家登陆

图片 6

 

Bee工具的利用

 安装到位Bee后,在调节台输入bee

Bee is a tool for managing beego framework.

Usage:

    bee command [arguments]

The commands are:

    new         Create a Beego application
    run         run the app and start a Web server for development
    pack        Compress a beego project into a single file
    api         create an API beego application
    hprose      create an rpc application use hprose base on beego framework
    bale        packs non-Go files to Go source files
    version     prints the current Bee version
    generate    source code generator
    migrate     run database migrations
    fix         fix the beego application to make it compatible with beego 1.6

Use "bee help [command]" for more information about a command.

Additional help topics:


Use "bee help [topic]" for more information about that topic.

驷不比舌有上述十一个指令,上边说多少个常用的通令

beego的安装

beego框架的设置格局很轻松,使用一下限令就可以

go get github.com/astaxie/beego

go语言在这点小编觉着是做的可比好的,直接用命令就能够直接下载安装好。当然上述指令需求git扶助。客商同样可以在github上下载源代码然后放置到GOPATH下的src/github.com/astaxie/目录下,然后选拔go install就可一向设置。

beego还会有贰个工具bee用于救助神速支付beego项目,通过bee能够很轻巧的实行创设、热编写翻译、开荒、测验和布局。
bee工具得以动用go指令间接下载安装:

go get github.com/beego/bee

有个别顾客恐怕在安装了bee工具后运转bee时会遭遇被系统killed的难点,那么些难点小编也没评释实际是何等原因,碰着这种难点的客商可以向来从github.com/beego/bee上clone下载最新源码到GOPATH\src\github.com\beego\下然后checkout
hot-fix-support分支,然后实行go install指令展开手动安装就可以。
执行

bee version

若出现以下音信便表明安装成功且运营符合规律化:

$ bee version
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.8.1

├── Beego     : Beego is not installed. Please do consider installing it first: https://github.com/astaxie/beego
├── GoVersion : go1.8
├── GOOS      : darwin
├── GOARCH    : amd64
├── NumCPU    : 4
├── GOPATH    : /Users/alpher/go
├── GOROOT    : /usr/local/Cellar/go/1.8/libexec
├── Compiler  : gc
└── Date      : Wednesday, 5 Apr 2017

 new命令

  new 命令是新建一个 Web 项目,我们在命令行下推行 bee new <项目名> 就能够成立三个新的品类。可是注意该命令必需在 $GOPATH/src 下施行。最终会在 $GOPATH/src 相应目录下转移如下目录结构的档案的次序:

bogon:src zuxingyu$ bee new cnblogs
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.5.2
2016/10/31 22:16:11 [INFO] Creating application...
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/conf/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/models/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/js/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/css/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/static/img/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/views/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/conf/app.conf
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers/default.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/views/index.tpl
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers/router.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests/default_test.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/main.go
2016/10/31 22:16:11 [SUCC] New application successfully created!

  那样二个beego框架的Go项目就生成了

创建beego项目

设置完以上中国人民解放军海军事工业程大学业具大家就能够直接通过bee new projectName来创设叁个beego框架的花色

$ bee new beegoDemo
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.8.1
2017/04/05 10:24:49 INFO     ▶ 0001 Creating application...
    create   /Users/alpher/go/src/beegoDemo/
    create   /Users/alpher/go/src/beegoDemo/conf/
    create   /Users/alpher/go/src/beegoDemo/controllers/
    create   /Users/alpher/go/src/beegoDemo/models/
    create   /Users/alpher/go/src/beegoDemo/routers/
    create   /Users/alpher/go/src/beegoDemo/tests/
    create   /Users/alpher/go/src/beegoDemo/static/
    create   /Users/alpher/go/src/beegoDemo/static/js/
    create   /Users/alpher/go/src/beegoDemo/static/css/
    create   /Users/alpher/go/src/beegoDemo/static/img/
    create   /Users/alpher/go/src/beegoDemo/views/
    create   /Users/alpher/go/src/beegoDemo/conf/app.conf
    create   /Users/alpher/go/src/beegoDemo/controllers/default.go
    create   /Users/alpher/go/src/beegoDemo/views/index.tpl
    create   /Users/alpher/go/src/beegoDemo/routers/router.go
    create   /Users/alpher/go/src/beegoDemo/tests/default_test.go
    create   /Users/alpher/go/src/beegoDemo/main.go
2017/04/05 10:24:49 SUCCESS  ▶ 0002 New application successfully created!

项目开创好后,大家须要步向到品种中,使用bee run projectName运营新创立的beego项目,使用bee run命令的指标是达到热编写翻译的意义,能实时查见到咱们修改的剧情效果。

$ bee run beegoDemo
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.8.1
2017/04/05 10:29:29 INFO     ▶ 0001 Using 'beegoDemo' as 'appname'
2017/04/05 10:29:29 INFO     ▶ 0002 Initializing watcher...
beegoDemo/controllers
beegoDemo/routers
beegoDemo
2017/04/05 10:29:31 SUCCESS  ▶ 0003 Built Successfully!
2017/04/05 10:29:31 INFO     ▶ 0004 Restarting 'beegoDemo'...
2017/04/05 10:29:31 SUCCESS  ▶ 0005 './beegoDemo' is running...
2017/04/05 10:29:31 [I] [asm_amd64.s:2197] http server Running on http://:8080

这么我们的利用就早就在8080端口运营了,访谈localhost:8080就足以见到如下效果了:

图片 7

 api命令

  上面的 new 命令是用来新建 Web 项目,可是非常多顾客采用 beego 来开发API 应用。所以这一个 api 命令便是用来创建 API
应用的,属于纯服务端业务。实践命令之后如下所示:

bogon:src zuxingyu$ bee api cnblogsApi
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.5.2
2016/10/31 22:19:51 [INFO] Creating API...
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/conf
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/tests
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/conf/app.conf
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/routers/
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers/object.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/controllers/user.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/tests/default_test.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/routers/router.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models/object.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/models/user.go
    create     /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogsApi/main.go
2016/10/31 22:19:51 [SUCC] New API successfully created!

  从地方的目录大家能够观望和 Web 项目相比较,少了 static 和 views
目录,多了三个 test 模块,用来做单元测量试验的。

  同一时候,该命令还匡助部分自定义参数自动连接数据库成立连锁model和controller:
  bee api [appname] [-tables=“”] [-driver=mysql]
[-conn=root:@tcp(127.0.0.1:3306)/test]
  如若conn参数为空则缔造二个示范项目,不然将基于链接消息链接数据库创立项目。

 run命令

  bee run 命令是监督 beego
的类型,通过 fsnotify监控文件系统。不过注意该命令必须在$GOPATH/src/appname下执行。 

 

bogon:src zuxingyu$ cd cnblogs
bogon:cnblogs zuxingyu$ bee run
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.5.2
2016/10/31 22:22:30 [INFO] Using 'cnblogs' as 'appname'
2016/10/31 22:22:30 [INFO] Initializing watcher...
2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/controllers)
2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs)
2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/routers)
2016/10/31 22:22:30 [TRAC] Directory(/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/tests)
2016/10/31 22:22:30 [INFO] Start building...
2016/10/31 22:22:34 [SUCC] Build was successful
2016/10/31 22:22:34 [INFO] Restarting cnblogs ...
2016/10/31 22:22:34 [INFO] ./cnblogs is running...
2016/10/31 22:22:34 [I] [asm_amd64.s:2086] http server Running on http://:8080

  注意:Linux/Mac Os 在命令行运营时,甘休间接用Ctrl + C 截至

 pack命令    

    pack 目录用来宣布应用的时候打包,会把项目打包成 zip
包,那样大家布置的时候一贯把包装之后的花色上传,解压就能够配备了。大概在后头专门的学问中会使用Docker去做容器。具体的在背后使用后告知我们。

bogon:cnblogs zuxingyu$ bee pack
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.5.2
2016/10/31 22:28:03 Packaging application: /Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs
2016/10/31 22:28:03 Building application...
2016/10/31 22:28:03 Env: GOOS=darwin GOARCH=amd64
2016/10/31 22:28:06 Build successful
2016/10/31 22:28:06 Excluding relpath prefix: .
2016/10/31 22:28:06 Excluding relpath suffix: .go:.DS_Store:.tmp
2016/10/31 22:28:07 Writing to output: `/Users/zuxingyu/Documents/GoWorkSpace/src/cnblogs/cnblogs.tar.gz`

  使用命令后会打出三个tar.gz包。归入Linux/Mac OS 解压就能够。

 version 命令

  这几个命令是动态获取bee、beego和Go的本子,那样一旦客商出现谬误,能够通过该命令来查阅当前的本子,没啥要求表明的。只是查看下当前版本。

bogon:cnblogs zuxingyu$ bee version
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.5.2

├── Beego     : 1.7.1
├── GoVersion : go1.7.1
├── GOOS      : darwin
├── GOARCH    : amd64
├── NumCPU    : 4
├── GOPATH    : /Users/zuxingyu/Documents/GoWorkSpace
├── GOROOT    : /usr/local/go
├── Compiler  : gc
└── Date      : Monday, 31 Oct 2016

 generate 命令

   那是一个第一的通令,极其是生成api项目时,generate
docs能够生成swagger的文书档案。

bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
    The generate scaffold command will do a number of things for you.
    -fields: a list of table fields. Format: field:type, ...
    -driver: [mysql | postgres | sqlite], the default is mysql
    -conn:   the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
    example: bee generate scaffold post -fields="title:string,body:text"

bee generate model [modelname] [-fields=""]
    generate RESTful model based on fields
    -fields: a list of table fields. Format: field:type, ...

bee generate controller [controllerfile]
    generate RESTful controllers

bee generate view [viewpath]
    generate CRUD view in viewpath

bee generate migration [migrationfile] [-fields=""]
    generate migration file for making database schema update
    -fields: a list of table fields. Format: field:type, ...

bee generate docs
    generate swagger doc file

bee generate test [routerfile]
    generate testcase

bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3]
    generate appcode based on an existing database
    -tables: a list of table names separated by ',', default is empty, indicating all tables
    -driver: [mysql | postgres | sqlite], the default is mysql
    -conn:   the connection string used by the driver.
             default for mysql:    root:@tcp(127.0.0.1:3306)/test
             default for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
    -level:  [1 | 2 | 3], 1 = models; 2 = models,controllers; 3 = models,controllers,router

 那些都以本身日前常用的命令。假如有另外命令以往能够开个专项论题专门尝试那个命令。

 

相关文章