当前位置:网站首页>go defer的使用说明
go defer的使用说明
2022-07-04 19:42:00 【码农竟在我身边】
defer是go中一种延迟调用机制,defer后面的函数只有在当前函数执行完毕后才能执行,通常用于释放资源。
defer遵循先进后出的原则,类似于栈的结构。
为什么要把defer设计成这种机制?
因为后申请的资源和可能对前面申请的资源有依赖。如果先将前面申请的资源释放掉了。对于后面的资源可能会造成影响。所以先释放后申请的资源,再释放前面申请的资源
func main() {
defer func() {
fmt.Println("w")
if err := recover(); err != nil {
fmt.Println(err)
}
}()
f()
}
func f() {
fmt.Println("a")
panic("error defer")
}
边栏推荐
- Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
- 语义化标签的优势和块级行内元素
- Flet教程之 06 TextButton基础入门(教程含源码)
- word中使用自动插入题注功能
- 看腾讯大老如何做接口自动化测试
- 九齐单片机NY8B062D单按键控制4种LED状态
- Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
- MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
- LeetCode+ 81 - 85 单调栈专题
- Idea case shortcut
猜你喜欢

电脑共享打印机拒绝访问要怎么办

二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】

Idea configuration standard notes

面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧

精选综述 | 用于白内障分级/分类的机器学习技术

阿里测试师用UI自动化测试实现元素定位

MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
Practical examples of node strong cache and negotiation cache

Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"

Sword finger offer II 80-100 (continuous update)
随机推荐
Play the music of youth
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
ACM组合计数入门
acwing 3302. 表达式求值
word中使用自动插入题注功能
From automation to digital twins, what can Tupo do?
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
See how Tencent does interface automation testing
Go notes (1) go language introduction and characteristics
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
Advantages of semantic tags and block level inline elements
Record the online bug solving list (unfinished to be continued 7/4)
托管式服务网络:云原生时代的应用体系架构进化
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
RFID仓储管理系统解决方案的优点
In operation (i.e. included in) usage of SSRs filter
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
Idea configuration standard notes
BFC面试简述
go语言笔记(4)go常用管理命令