当前位置:网站首页>面试官:说说 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 语言里面没有类似的语法糖。
你学废了么?
边栏推荐
- 我也是醉了,Eureka 延迟注册还有这个坑!
- HCIP第一天实验
- 如何看一本书
- What financial products can you buy to make money with only 1000 yuan?
- Chapter VI succession
- 无聊发文吐槽工作生活
- 11、照相机与透镜
- [PHP pseudo protocol] source code reading, file reading and writing, and arbitrary PHP command execution
- STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解
- Redis cluster deployment based on redis6.2.4
猜你喜欢

Text translation software - text batch translation converter free of charge

我也是醉了,Eureka 延迟注册还有这个坑!

11、照相机与透镜

Customize MVC project login registration and tree menu

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

Don't believe these "rumors" in the process of preparing for the exam!

Hcip notes 12 days

I2C communication - sequence diagram

Redis源码与设计剖析 -- 16.AOF持久化机制

Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes
随机推荐
ACL 2022 | 基于最优传输的对比学习实现可解释的语义文本相似性
Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
[Hardware Engineer] can't select components?
How to delete Microsoft Pinyin input method in win10
How to install govendor and open a project
I2C communication - sequence diagram
The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!
Redis源码与设计剖析 -- 17.Redis事件处理
I'm also drunk. Eureka delayed registration and this pit!
11. Camera and lens
Starting from business needs, open the road of efficient IDC operation and maintenance
Go语言系列:Go从哪里来,Go将去哪里?
ROS learning notes (IV) ROS cannot solve rosdep init or update
Is there a method in PostgreSQL that only compiles statements but does not execute them?
实时黄金交易平台哪个可靠安全?
Hcip notes 11 days
Pymongo saves data in dataframe format (insert_one, insert_many, multi-threaded saving)
[Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
[Hardware Engineer] about signal level driving capability