Preface:

Hi, I’m Asong and this is my fifth original article. I have been busy with graduation recently. I have had many things for a long time. Now that I’m almost done, I’m going to update the article. In recent days, I have studied beego framework, read an official document and an official sample. Based on this, I have modified it, added the database and improved some functions, which can be used as a small introductory demo for learning. The code has been uploaded to Github repository (github.com/sunsong2020…

Projects show

We can clone the project locally on Github and type the following command:

Here need to pay attention to the installation of bee tools may be a problem, here need to test, in the terminal input bee, if the following picture appears, there is no problem, if not, then the installation of bee tools error, you can see my blog (blog.csdn.net/qq_39397165…

After the environment is installed, we can run the project. Enter Bee Run in the terminal to run the project. The successful operation is shown in the picture below:

Open the browser and enter localhost:8080, you can see the following interface:

If there is no account, we can register first. Click the registration button to enter the registration interface and enter the relevant information to complete the registration.

Then enter your phone number and password to enter the chat room. Two people can chat in the chat room, as shown in the screenshot below:

As you can see in the chat logs, this is just such a simple project that you can quickly familiarize yourself with the BeeGo framework. Some knowledge points are introduced below.

WebSocket

Polling: The client sends Ajax requests at regular intervals. This approach is simple to implement, but consumes bandwidth and server resources, requiring frequent establishment and release of TCP connections. Long polling: Different from ordinary polling, the server will hold the request and do not respond immediately after receiving it. The server will respond and close the connection until there is a message update, and the client will re-initiate the request after processing the response. There are no useless requests compared to normal polling, but there is also a cost to the server to stay connected, which is no different from normal polling if the server data changes frequently.

Long connections: Embed a hidden IFrame in the page and set its SRC to a long connection request so that the server can continuously send data to the client, which has the same advantages and disadvantages as long polling.

These technologies have very big shortcoming, cause the server overhead is very large, so I finally chose the websocket, only need a handshake, can complete the connection between the client and service, through the way of data frames after communication on this connection, the communication is a two-way street, the server can also complete the initiative to send a message to the client, This also reduces a lot of business logic and makes coding easier.

Golang using websocket is also very simple, only need to import the package we can use the “github.com/gorilla/websocket”. The specific use of this project is not described here, but take a look at the code example of this project:

beego

Here I use beego framework, this framework is very simple to use, after all, it is developed by Chinese, there are complete Chinese documents, according to the document we can quickly reach the entry level. Beego is a fast development Go application HTTP framework, he can be used to quickly develop API, WEB and back-end services and other applications, is a RESTful framework, combined with Go itself some features and design a framework. Beego. me/quickstart… Here, I use the tool bee of the framework to create a project. The project is a Web project, and the entire project architecture is available after it is created. The project architecture is as follows:

The models, Views, and Controllers directories conform to the MVC architecture. The main. Go directory is an entry file, and the routers directory is used to configure routes.

mysql

The Mysql database is used here. There are only two tables created here, one user table and one message table. The user table is used to store user information, and the message table is used to store message records. Because it is a hands-on project, there are no more tables, only entry level. The table structure is as follows:

Beego ORM is introduced here. Beego ORM is a powerful Go LANGUAGE ORM framework. Three database drivers are supported: Mysql, PostgreSQL, and Sqlite3. Mysql is used here. Enter the following command to complete the installation:

go get github.com/astaxie/beego/orm
Copy the code

After the installation, we started to use it. Since Golang has init functions, we can register the database driver and register an alias database in the init function to complete the database driver registration. A code example is as follows:

Here we use the RunSyndb method. Instead of manually creating a database table, it creates it based on the table structure in the structure, which improves efficiency.

There is another point that needs to be noted here. If we need our project to be complex and require advanced queries, we must register the model, otherwise it will not be able to use it. Model registration is done using the RegisterModel. The following is an example:

Database introduction so much, the specific use of reference to the official documents.

conclusion

The overall difficulty of this project is general, it is an entry level project, other knowledge points are not summarized here. The code is written in detail, with comments, and can be understood. Interested partners, quick action, their own chat room to write a small demo, I believe it is very easy for you. This is the end of today’s sharing, thanks for watching!!

You can follow my official account and search for Golang DreamWorks, or scan the QR code below. I will publish quality articles regularly, mainly for Golang language knowledge, framework learning, interview arrangement and so on, you want to learn here!!