当前位置:网站首页>Goweb开发之Beego框架实战:第三节 程序执行流程分析
Goweb开发之Beego框架实战:第三节 程序执行流程分析
2022-07-28 16:20:00 【qfliweimin】
执行流程分析
Go语言执行的时候是执行main包下面的init函数,main函数依次执行。因此,先找到main.go文件。

如上图就是main.go文件内容,那么代码如何执行呢?首先,import导入了两个包,一个是routers,一个是beego。而在routers包前面,可以看到有一个“_”,这表明是引入routers包,并执行init方法。这里涉及一个知识点,就是Go语言的执行过程,这里给大家一张程序执行流程图:

根据上图,可以得知程序首先到routers包下执行init方法。到router.go里面看一下具体的代码:

可以看到在router.go文件中有一个init方法,看到beego.Router()这句代码。router表示的是路由的意思,这个函数的功能是映射 URL 到 controller,第一个参数是 URL (用户请求的地址),这里注册的是 /,也就是访问的不带任何参数的 URL,第二个参数是对应的 Controller,即将把请求分发到那个控制器来执行相应的逻辑,现在去这里设置的MainController中去看一下有什么方法:

MainController结构体及函数声明在default.go文件中。而这里就看到一个Get方法,方法中有三行代码。
上文在浏览器中访问的是:http://localhost:8080,这是一个get请求,请求到了后台以后,什么请求参数都没有,因此,就会被“/”拦截,执行到MainController中的代码,因为是get请求,所以这里自动找到Get函数并进行执行。
在get函数里面,有三句代码,前两句c.Data[]= ""表示设置返回的数据字段及内容,最后一句c.TplName表示设置处理该请求指向某个模板文件,这里指向了index.tpl,那么index.tpl文件在哪里呢?可以查看项目目录结构,在views下面,views下面存放一些模板文件。
- 模板文件
简单解释一下,通常的页面都是使用静态的html+css+js等这些静态代码来进行页面的布局,页面效果控制等,而把页面的数据使用变量表示,这样,在进行页面展示的时候,就能够自动的填充页面里面的变量的值;这些静态的代码文件统称为模板文件。每个模板文件就是像一个模板一样,样式效果都固定,只是根据数据不一样进行渲染和展示。
beego.Run()逻辑
init方法分析完毕后,程序会继续往下执行,就到了main函数,在main函数中执行:beego.Run()代码。分析一下Run代码的逻辑,在Run方法内部,主要做了几件事:
- 1)解析配置文件,也就是我们的app.conf文件,比如端口,应用名称等信息。
- 2)检查是否开启session,如果开启session,就会初始化一个session对象。
- 3)是否编译模板,beego框架会在项目启动的时候根据配置把views目录下的所有模板进行预编译,然后存放在map中,这样可以有效的提高模板运行的效率,不需要进行多次编译。
- 4)监听服务端口。根据app.conf文件中的端口配置,启动监听。
边栏推荐
- [deep learning]: model evaluation and selection on the seventh day of pytorch introduction to project practice (Part 1): under fitting and over fitting (including source code)
- 微服务架构-服务注册中心和服务网关(6.8) (转载)
- Difference between reconnaissance aircraft and early warning aircraft
- MySQL installation tutorial
- 如何在构建阶段保护镜像安全
- [deep learning]: day 1 of pytorch introduction to project practice: data operation and automatic derivation
- Unity shader realizes mirror effect with rendered texture
- Classroom attendance system based on QT design (using RDS for MySQL cloud database)
- 累计出货130亿颗Flash,4亿颗MCU!深度解析兆易创新的三大产品线
- HTAP comes at a price
猜你喜欢

HTAP是有代价的
![[deep learning]: day 1 of pytorch introduction to project practice: data operation and automatic derivation](/img/4e/a41eee56fc0e8d3089f105bcb63155.png)
[deep learning]: day 1 of pytorch introduction to project practice: data operation and automatic derivation

Probability theory and mathematical statistics Chapter 1

火了 2 年的服务网格究竟给微服务带来了什么?(转载)

总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对

Comprehensively design an oppe homepage -- page service part
![[deep learning]: model evaluation and selection on the seventh day of pytorch introduction to project practice (Part 1): under fitting and over fitting (including source code)](/img/19/18d6e94a1e0fa4a75b66cf8cd99595.png)
[deep learning]: model evaluation and selection on the seventh day of pytorch introduction to project practice (Part 1): under fitting and over fitting (including source code)

数据库故障容错之系统时钟故障

Unity shader realizes water wave effect with noise texture

UNIQUE VISION Programming Contest 2022(AtCoder Beginner Contest 248)G. GCD cost on the tree
随机推荐
Rsync 服务部署与参数详解
Huawei mate 40 series exposure: large curvature hyperboloid screen, 5nm kylin 1020 processor! There will also be a version of Tianji 1000+
RE14: reading paper illsi interpretable low resource legal decision making
Realize the reset function of steering wheel UI with touch rotation and finger departure in unity
[deep learning]: the second day of pytorch introduction to project practice: realize linear regression from zero (including detailed code)
Rsync service deployment and parameter details
Algorithm learning: leetcode interview question 09. implement queue with two stacks
如何使用Fail2Ban保护WordPress登录页面
Create a self-organizing / safe / controllable Lora network! Semtech responded for the first time to the impact of the "new regulations of the Ministry of industry and information technology"
Question making note 3 (two point search)
It is said that NVIDIA has held talks with Softbank and will offer more than US $32billion to acquire arm
Pytorch Foundation: similarities and differences between torch.mul, torch.mm and torch.matmul
HTAP comes at a price
Cf/atc/lc topic score website
技术分享 | 误删表以及表中数据,该如何恢复?
Unity shader realizes water wave effect with noise texture
[deep learning]: day 5 of pytorch introduction to project practice: realize softmax regression from 0 to 1 (including source code)
The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
Message Passing for Complex Question Answering over Knowledge Graphs
Comprehensively design an oppe homepage -- after sales service of the page