Beego Study notes--Start

Source: Internet
Author: User

Beego Introduction

Beego is a fast-developing HTTP framework for GO applications that can be used to quickly develop applications such as APIs, WEB, backend services, and is a restful framework designed to be inspired by the three frameworks of tornado, Sinatra, Flask, But a framework designed to incorporate some of the features of Go itself (interface, struct inheritance, etc.).

Architecture of the Beego

The overall design architecture of the Beego is as follows:

Built on the basis of eight independent modules, Beego is a highly decoupled framework. When the design of the Beego is to consider the function of modular, even if the user does not apply Beego HTTP logic, but also can be used in these standalone modules, for example, you can use the cache module to do your caching logic, using the log module to record your operation information, Use the Config module to parse your files in various formats, so not just in beego development, your socket game development is also a useful module, which is why Beego is popular for a reason. If you have played Lego, you should know that a lot of high-level things are a piece of building blocks, and design beego, these modules are building blocks, high-level robot is beego. The functionality of these modules and how they are used will be described in a later document.

Beego's execution logic

Since Beego is built on these modules, what is the logic of his execution? Beego is a typical MVC architecture, and his execution logic looks like this:

BEEGO Project Structure

The directory for the General Beego project is as follows:

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

From the above directory structure we can see the structure of M (models directory), V (views directory), C (Controllers directory), main.go is the entry file.

The above is an introduction to Beego's official documentation and its components.        In the back of the learning process, I will take apart the 8 basic modules, aside from MVC, part of the study.                The following first set up a simple command-style test entry program to facilitate later learning when the output.        For the installation of Beego here is not much to say, Baidu, can find a lot of relevant technical blog, official documents also have a very detailed introduction. Environment: Ide:intellij idea running system: there is no special description behind Mac OS, this is the background. 1, new projects such as, Idea->file->project

Select Go-> Click Next to select Go version->next Select the project path, give your project name, here is simply named Beegotest Bar, then click Finish

Here is the directory structure we created, which is an empty project

Then create a main.go file in the root directory as our entry file. And then simply output a sentence of "Beego test"
" FMT " Func Main () {       fmt. Println ("beego test")}

Click the terminal tag in the lower left corner of the editor to display a terminal window, and the directory has been switched to the current directory, and we can then command to execute our code here.

In the terminal input command: Go run main.go Enter execution, for example, already output Beego test in our code

Our program's command format is the cmd params, the command and parameters are separated by a space, the following write a simple help function
Func help (args []string)int{       fmt. Println (' Command: Help       (h)       ')       return0}

But how do you get this function to execute this method when we enter help, or the H command? This is the time to do two steps, the first is where the command needs to be entered, and the second is to switch the command to our function. We add the function of command input in the main function.
Func Main () {       r:= Bufio. Newreader (OS. Stdin)       FMT. Print ("")       B, _, _:= r.readline () line       :string  (b)       tokens:"")       FMT. PRINTLN (Tokens)}

The results of the operation, such as, where the red box is the content of their own hand lost.

Next is the second step, how to call the relevant function through the contents of our input, below we will add a getcommandhandlers function, as follows:
Func getcommandhandlers () map[string]func (args []stringint  {       return map[string]func ([]stringint{              "help"  : Help,              'h': Help    ,       }}

This function returns a map, where key is the command we define, value is the function we define for this command, and the definition of this function must be []string], the return type is int, the return is a number other than 0, then exit the program, and 0 continues. If additional commands are added later, the mapping is added to this function. Let's continue to transform the main function so that it calls this function:
Func Main () {r:=Bufio. Newreader (OS. Stdin) Handlers:=getcommandhandlers () help (nil) for{fmt. Print ("command>") B, _, _:=R.readline () line:=string(b) Tokens:= Strings. Split (Line," ")              ifHandler, OK: = handlers[tokens[0]]; ok{ret:=handler (tokens)ifRet! =0{                             Break                     }              }Else{fmt. Println ("Unknown Command:", tokens[0])              }       }}
The results of the operation are as follows:

Below we are adding an exit command. The complete code is as follows:
Package Mainimport ("Bufio"       "FMT"       "OS"       "Strings") Func Main () {r:=Bufio. Newreader (OS. Stdin) Handlers:=getcommandhandlers () help (nil) for{fmt. Print ("command>") B, _, _:=R.readline () line:=string(b) Tokens:= Strings. Split (Line," ")              ifHandler, OK: = handlers[tokens[0]]; ok{ret:=handler (tokens)ifRet! =0{                             Break                     }              }Else{fmt. Println ("Unknown Command:", tokens[0])}}}func getcommandhandlers () map[string]func (args []string)int {       returnmap[string]func ([]string)int{              " Help": Help,"h": Help,"quit": Quit,"Q": Quit,}} Func help (args []string)int{fmt. Println (' Command:help (h) Quit (q) ')return 0}func Quit (args []string)int{       return 1}

The results of the operation are as follows:

Beego Study notes--Start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.