Beego profile

Beego is an HTTP framework for rapid development of Go applications. It can be used for rapid development of API, Web and back-end services and other applications. Beego is a RESTful framework, mainly inspired by Tornado, Sinatra and Flask frameworks. But it is a framework designed by combining some features of Go itself (interface, struct embedding, etc.).

Beego’s design is a thinking of building blocks, which is modular construction and a highly decoupled framework. With modular development, you can use these individual modules even if you don’t understand Beego’s HTTP logic. For example, you can use the cache module to do your cache logic; Use the log module to record your operation information; Use the Config module to parse your files in various formats. So beego can be used not only for HTTP applications, but also as a useful module in your socket game development, which is one of the reasons why Beego is so popular.

Beego’s execution logic

Beego is a typical MVC architecture, and its execution logic is shown in the figure below:

The installation of beego

1. Install bee

// Used to install beego get github.com/beego/bee/v2Copy the code

Possible problems:

$go get github.com/beego/bee
go get: module github.com/beego/bee: Get "https://proxy.golang.org/github.com/beego/bee/@v/list": dial tcp 216.58.200.49:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Copy the code

This is because golang’s mirror path is the official path: set GOSUMDB=sum.golang.org, which is slow and error-prone in use.

If GOSUMDB is set to env, go env is set to GOSUMDB.

go env -w GOSUMDB=off
Copy the code

Then set the proxy to the domestic mirror, with go mod fast, do not have to climb the wall.

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

Copy the code

Configuration environment:

In Windows10 configuration deployment is: this computer -> properties -> advanced system Settings -> environment variables

View the bee version.

$beeVersion... | ___ \ | | _ / / ___ ___ | ___ \ / _ \ _ \ | | _ / / | __ / | __ / \ _____ / ___ | \ ___ | v2.0.2 ├ ─ ─ Beego: Beego is not installed. Pleasedo consider installing it first: https://github.com/beego/beego/v2. If you are using go mod, and you don't install the beego under $GOPATH/src/github.com/astaxie, just ignore this. ├ ─ ─ GoVersion: go1.16.2 ├ ─ ─ GOOS: Windows ├ ─ ─ GOARCH: amd64 ├ ─ ─ NumCPU: 4 ├ ─ ─ GOPATH: C: \ Users \ liu \ go ├ ─ ─ GOROOT: D: \ environment \ go ├ ─ ─ the Compiler: Garbage └─ Date: Saturday, 27 Mar 2021Copy the code

Create a project

Beego projects are basically created using bee commands, so make sure you have bee tools and BeeGo installed before creating a project. If you haven’t already, check out beego installation and Bee tool installation. Now that we are ready to create the project, open the terminal and go to the directory where $GOPATH/ SRC is located:

$beeNew bee_01 2021/03/27 12:33:23 INFO ▶ 0001 Generate new project support go modules. 2021/03/27 12:33:23 INFO ▶ 0002 Creating application... create D:\golang\bee_01\go.mod create D:\golang\bee_01\ create D:\golang\bee_01\conf\ create D:\golang\bee_01\controllers\ create D:\golang\bee_01\models\ create D:\golang\bee_01\routers\ create D:\golang\bee_01\tests\ create D:\golang\bee_01\static\ create D:\golang\bee_01\static\js\ create D:\golang\bee_01\static\css\ create D:\golang\bee_01\static\img\ create D:\golang\bee_01\views\ create D:\golang\bee_01\conf\app.conf create D:\golang\bee_01\controllers\default.go create D:\golang\bee_01\views\index.tpl create D:\golang\bee_01\routers\router.go create D:\golang\bee_01\tests\default_test.go create D:\golang\bee_01\main.go 2021/03/27 12:33:23 SUCCESS ▶ 0003 New Application successfully created!Copy the code

Install related plug-ins:

Go mod download github.com/beego/beego/v2 go get github.com/beego/beego/v2/server/[email protected] bee API bee_01 / / API commandsCopy the code

The biggest problem we had when developing Go project was that we often had to manually compile and run it. Bee Run command was a project to monitor BeeGo, and fsnotify was used to monitor the file system. In this way, we can see the effect of project modification in real time during the development process:

Bee run... | ___ \ | | _ / / ___ ___ | ___ \ / _ \ _ \ | | _ / / | __ / | __ / \ _____ / ___ | \ ___ | v2.0.2 2021/03/27 12:39:58 WARN ▶ 0001 Running Application Outside of GOPATH 2021/03/27 12:39:58 INFO ▶ 0002 Using'bee_01' as 'appname'2021/03/27 12:39:58 INFO ▶ 0003 Initializing Watcher... At the same time, please contact us for more information on these areas. Please contact us for more information on these areas. 2021/03/27 12:40:01 INFO ▶ 0005 Restarting'bee_01.exe'. 2021/03/27 12:40:01 SUCCESS ▶ 0006'./bee_01.exe'is running... 2021/03/27 12:40:03.234 [I] [parser. Go :413] Generate router from comments 2021/03/27 12:40:03.249 [I] [server. HTTP server Running on http://:8080 2021/03/27 12:40:30. 947 [D] [router. Go: 955] | : : 1 | 200 | | ms 2.5006 match | GET/r: / 2021/03/27 12:40:31. 062 [D] [router. Go: 955] | : : 1 931.3 (including 200 | | s | match | GET/static/js/reload. Min. JsCopy the code

Open test in browser:

Ok.