当前位置:网站首页>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")
}
边栏推荐
- Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem
- In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
- Length of the longest integrable subarray
- How does win11 search for wireless displays? Win11 method of finding wireless display device
- 接口设计时的一些建议
- NetCore3.1 Json web token 中间件
- Practical examples of node strong cache and negotiation cache
- What is the development of block hash quiz game system? Hash quiz game system development (case mature)
- Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
- 企业数字化转型最佳实践案例:基于云的数字化平台系统安全措施简介与参考
猜你喜欢
Related concepts of federal learning and motivation (1)
RFID仓库管理系统解决方案有哪些功能模块
Win11无法将值写入注册表项如何解决?
精选综述 | 用于白内障分级/分类的机器学习技术
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
字节测试工程师十年经验直击UI 自动化测试痛点
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
看腾讯大老如何做接口自动化测试
Flet教程之 05 OutlinedButton基础入门(教程含源码)
How to adapt your games to different sizes of mobile screen
随机推荐
Understand the reading, writing and creation of files in go language
Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
Why is TCP three handshakes and four waves
哈希表、哈希函数、布隆过滤器、一致性哈希
Go language notes (4) go common management commands
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
go笔记(1)go语言介绍以及特点
Go notes (1) go language introduction and characteristics
PermissionError: [Errno 13] Permission denied: ‘data.csv‘
Hands on deep learning (III) -- convolutional neural network CNN
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
LeetCode 8. 字符串转换整数 (atoi)
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
九齐NY8B062D MCU规格书/datasheet
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
QT writing the Internet of things management platform 38- multiple database support
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
idea大小写快捷键
托管式服务网络:云原生时代的应用体系架构进化
Go language notes (2) some simple applications of go