当前位置:网站首页>面试官:说说 log.Fatal 和 panic 的区别
面试官:说说 log.Fatal 和 panic 的区别
2022-07-25 17:32:00 【小锟哥哥】
要熟悉两者区别之前,需要先了解 os.Exit() 函数。
我这里截一张 Go 源码里面的解释:
我们需要注意两点:
1、应用程序会被马上退出
2、defer 函数不会被执行
你可以理解这就是直接 kill -9 程序,不给任何喘气的机会。
现在我们再来看看 log.Fatal 函数的定义:
整体来说,他在 os.Exit() 之前做了一个日志输出。
他的流程可以理解是:
1、打印输出内容
2、退出应用程序
3、defer 函数还是不会执行
最后我们来看下 panic 的定义:
我们需要注意的是:
1、当前函数立刻会停止执行(不是主程序)
2、defer 函数不会执行
3、返回给调用者 caller
4、调用函数假装也收到了 panic 函数,从而他们也会执行以上的操作
5、递归执行,直到最上层函数,如果都没函数处理这个异常,应用程序就会停止
这和其他语言里面的 try catch 很像,但是 Go 语言里面没有类似的语法糖。
你学废了么?
边栏推荐
- 「数字安全」警惕 NFT的七大骗局
- Wu Enda logistic regression 2
- 【Cadence Allegro PCB设计】error: Possible pin type conflict GND/VCC Power Connected to Output
- With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
- Lvgl 7.11 tileview interface cycle switching
- 论文阅读_多任务学习_MMoE
- Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
- Redis源码与设计剖析 -- 16.AOF持久化机制
- 实时黄金交易平台哪个可靠安全?
- POWERBOARD coco! Dino: let target detection embrace transformer
猜你喜欢

Redis源码与设计剖析 -- 17.Redis事件处理

I2C communication - sequence diagram
![[solution] the Microsoft edge browser has the problem of](/img/47/7e20a4f1e04577153e7cf0a6c61f26.png)
[solution] the Microsoft edge browser has the problem of "unable to access this page"

Virtual memory management

一篇文章了解超声波加湿器

How to install govendor and open a project

Chapter VI succession

Wu Enda logistic regression 2

用秩讨论线性方程组的解/三个平面的位置关系
![[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification](/img/22/01297d28e5bfb105fc65ee29248a7c.png)
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
随机推荐
MySQL数据库中去重与连接查询的方法
I want to manage money. I don't understand. Is there a principal guaranteed financial product?
【硬件工程师】DC-DC隔离式开关电源模块为什么会用到变压器?
服务器端架构设计期末复习知识点总结
new与malloc
Does PgSQL have a useful graphical management tool?
Crawler framework crawler
Box selection screenshot shortcut key of win10
Is there a principal guaranteed product for financial management?
Redis源码与设计剖析 -- 17.Redis事件处理
Ultimate doll 2.0 | cloud native delivery package
ROS learning notes (IV) ROS cannot solve rosdep init or update
How to install govendor and open a project
世界各地的标志性建筑物
什么是元宇宙Gamefi链游系统开发?Gamefi元宇宙NFT链游系统开发应用案例及分析
哈夫曼树的构建
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
Is it safe to open a futures account online? How to apply for a low handling fee?
[solution] the Microsoft edge browser has the problem of "unable to access this page"
虚拟内存管理