Beego Introduction
Beego I think it's an MVC web framework that's easier for go beginners to get started with. Easy to understand, the most important thing is to provide a Chinese document, which for my English ability than the poor person is the gospel.
Beego's official website introduces Beego: "Beego is a fast-developing HTTP framework for GO applications that can be used to quickly develop applications such as APIs, Web and backend services, and is a RESTful framework designed to be inspired by tornado, Sinatra and Flask are three frameworks, but a framework designed to incorporate some of the features of Go itself (interface, struct embedding, etc.). It can be seen that Beego can quickly develop API interface, can realize the various applications of the service side
Beego official website
Beego Official website: https://beego.me/
Github:https://github.com/beego
Beego Installation
Beego contains some sample applications to help you learn and use the Beego application framework.
You need to install Go 1.1+ to ensure that all features are working properly.
You need to install the Beego and Bee development tools:
$ go get github.com/astaxie/beego$ go get Github.com/beego/bee
Attention:
- Beege and Bee are two concepts. Beego is the framework, bee is the tool, is the command.
- Before installing Beego, verify that the $GOPATH/bin is written to the go environment.
Use of bee Tools
After the installation completes Bee, enter the bee in the console
Bee is a tool formanaging Beego Framework. Usage:bee command [arguments]the commands are:new Create a Beego application Run run the app an D Start a WEB server forDevelopment Pack Compress a Beego project into a single file API create an API Beego application Hprose Create an RPC application with Hprose base on Beego Framework Bale packs Non-go files to go source files version prints the current Bee version generate source code generator MIGR Ate run database migrations fix the Beego application to make it compatible with Beego1.6 Use"bee Help [command]" forMore information about a command. Additional Help Topics:use"bee Help [topic]" forMore information on that topic.
There are more than 10 commands, the following is a few common commands
New command
new
The command is to create a new Web project, and we can bee new <项目名>
execute it at the command line. Note, however, that the $GOPATH/src
command must be executed under. Finally, a $GOPATH/src
project with the following directory structure will be generated in the appropriate directory:
BOGON:SRC zuxingyu$Bee New cnblogs______| ___ | |_/ / ___ ___| ___ \ / _ \ / _ | |_/ /| __/| __/\____/ \___| \___| V1.5.22016/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.go2016/10/31 22:16:11 [SUCC] New application successfully created!
Such a Beego framework's Go project generates a
API commands
The above new
command is used to create a new Web project, but many users use Beego to develop API applications. So this api
command is used to create API applications, belonging to the service-only business. After executing the command, it looks like this:
BOGON:SRC zuxingyu$Bee API cnblogsapi______| ___ | |_/ / ___ ___| ___ \ / _ \ / _ | |_/ /| __/| __/\____/ \___| \___| V1.5.22016/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.go2016/10/31 22:19:51 [SUCC] New API successfully created!
From the above directory, we can see that compared with the WEB project, the static and the views of the directory, more than a test module, used to do unit testing.
The command also supports some custom parameters to automatically connect to the database to create the relevant model and controller:
Bee API [appname] [-tables= ""] [-driver=mysql] [-conn=root: @tcp (127.0.0.1:3306)/test]
If the conn parameter is empty, create a sample project, otherwise the project will be created based on the linked Information link database.
Run command
bee run
The command is to monitor the Beego project and monitor the file system through Fsnotify. Note, however, that the command must be
$GOPATH/src/appname
Under execution.
BOGON:SRC zuxingyu$CD cnblogsbogon:cnblogs Zuxingyu$Bee run______| ___ | |_/ / ___ ___| ___ \ / _ \ / _ | |_/ /| __/| __/\____/ \___| \___| V1.5.22016/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 successful2016/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
Note: When the Linux/mac Os runs at the command line, end directly with CTRL + C
Pack command
pack
when the catalog is used to publish the app, it packs the project into a zip package, so that when we deploy it, we can upload it directly after the package, and then unzip it and deploy it. You might use Docker to make containers in the back of your work. Specific in the later use to tell everyone.
bogon:cnblogs zuxingyu$ Bee pack______ | ___ | |_// ___ ___ | ___ \/_ \/ _ | |_//| __/| __/\____ /\___| \___| V1.5.22016/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:. /SPAN>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 '
A tar.gz package is typed after the command is used. Add Linux/mac OS to unzip.
Version command
This command is a dynamically acquired version of Bee, Beego, and go, so that once the user has an error, the command can be used to view the current version, and there is nothing to explain. Just look under the current version.
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 OCT 2016
Generate command
This is an important command, especially when building an API project, generate docs can generate swagger documents.
Bee Generate scaffold [scaffoldname] [-fields=] [] [-driver=mysql] [-conn=] Root: @tcp (127.0.0.1:3306)/test " ] the Generate scaffold command would DoA number of things forYou . -fields:a List of table fields. Format:field:type, ...-driver: [mysql | postgres | sqlite], thedefaultis MySQL-conn:the connection string used by the driver, thedefaultis 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 controllersbee Generate view [Viewpath] Generate CRUD ViewinchviewpathBee Generate migration [migrationfile] [ -fields= "" ] generate migration file forMaking database Schema update-fields:a List of table fields. Format:field:type, ... Bee Generate docs generate swagger doc filebee generate test [routerfile] generate testcase be e 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 ', ',defaultis empty, indicating all tables-driver: [mysql | postgres | sqlite], thedefaultis MySQL-conn:the Connection string used by the driver. default forMysql:root: @tcp (127.0.0.1:3306)/Testdefault forPostgres:postgres://postgres:[email protected]:5432/Postgres-level: [1 | 2 | 3], 1 = models; 2 = models,controllers; 3 = Models,controllers,router
These are all the commands I'm used to now. If you have other commands, you can start a special project to experiment with these commands.
Go Beego Frame Use notes (i)