当前位置:网站首页>面试官:说说 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 语言里面没有类似的语法糖。
你学废了么?
边栏推荐
- 接口自动化测试Postman+Newman+Jenkins
- 电子产品“使用”和“放置”哪个寿命更长??
- Using rank to discuss the solution of linear equations / the positional relationship of three planes
- Random talk on generation diffusion model: DDPM = Bayesian + denoising
- MySQL数据库中去重与连接查询的方法
- 第五章:流程控制
- [Hardware Engineer] about signal level driving capability
- I want to manage money. I don't understand. Is there a principal guaranteed financial product?
- The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
- Go语言系列:Go从哪里来,Go将去哪里?
猜你喜欢

Jenkins' file parameters can be used to upload files

Replicate swin on Huawei ascend910_ transformer

Using rank to discuss the solution of linear equations / the positional relationship of three planes

Technical difficulties and applications of large humanoid robots

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

Hcip notes 11 days

An article about ultrasonic humidifier

Chapter VI succession

"Digital security" alert NFT's seven Scams

Wu Enda logistic regression 2
随机推荐
How to rectify the unqualified EMC of electronic products?
stm32F407------SPI
stm32F407------SPI
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
Summary of 80 domestic database operation documents (including tidb, Damon, opengauss, etc.)
Don't believe these "rumors" in the process of preparing for the exam!
Google Earth engine - download the globalmlbuildingfootprints vector collection of global buildings
postgreSQL 密码区分大小写 ,有参数控制吗?
Replicate swin on Huawei ascend910_ transformer
I2C通信——时序图
【Cadence Allegro PCB设计】error: Possible pin type conflict GND/VCC Power Connected to Output
Chapter VI succession
第五章:流程控制
How to prevent the unburned gas when the city gas safety is alarmed again?
Random talk on generation diffusion model: DDPM = Bayesian + denoising
Jenkins' role based authorization strategy installation configuration
一篇文章了解超声波加湿器
做智能硬件要考虑的产品生命周期
Boring post roast about work and life
[Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?