当前位置:网站首页>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")
}
边栏推荐
- 黄金k线图中的三角形有几种?
- Automatic generation of interface automatic test cases by actual operation
- 强化学习-学习笔记2 | 价值学习
- Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
- Reinforcement learning - learning notes 2 | value learning
- Common verification rules of form components -1 (continuously updating ~)
- WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
- Why is TCP three handshakes and four waves
- What is the development of block hash quiz game system? Hash quiz game system development (case mature)
- What if the brightness of win11 is locked? Solution to win11 brightness locking
猜你喜欢
Flet教程之 04 FilledTonalButton基础入门(教程含源码)
字节测试工程师十年经验直击UI 自动化测试痛点
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Quelques suggestions pour la conception de l'interface
QT writing the Internet of things management platform 38- multiple database support
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
黄金k线图中的三角形有几种?
太方便了,钉钉上就可完成代码发布审批啦!
实操自动生成接口自动化测试用例
Leetcode+ 81 - 85 monotone stack topic
随机推荐
接口设计时的一些建议
RFID仓库管理系统解决方案有哪些功能模块
Sword finger offer II 80-100 (continuous update)
Understand the reading, writing and creation of files in go language
托管式服务网络:云原生时代的应用体系架构进化
Practical examples of node strong cache and negotiation cache
RFID仓储管理系统解决方案的优点
Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
LeetCode 871. Minimum refueling times
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
Go notes (1) go language introduction and characteristics
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
最长的可整合子数组的长度
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
QT writing the Internet of things management platform 38- multiple database support
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
Idea restore default shortcut key
Stack: how to realize the judgment of valid brackets?
What if win11u disk refuses access? An effective solution to win11u disk access denial