issues
search
OctopusLian
/
leetcode-solutions
LeetCode,LintCode,牛客网,企业题库,《Sword to Offer》,《Cracking the Coding Interview》题解
MIT License
4
stars
4
forks
source link
每日一题:go中,defer原理是什么?
#214
Closed
OctopusLian
closed
2 years ago
OctopusLian
commented
3 years ago
https://my.oschina.net/u/4553401/blog/4727173
什么是defer
defer是go语言提供的一种用于注册延迟调用的机制:让函数或者语句在当前函数执行完毕(包括return正常结束或者panic导致的异常结束)之后执行。
defer语句通常用于一些成对的操作场景,打开/关闭连接,加锁/解锁,打开文件/关闭文件等等
defer在一些需要回收资源的场景中非常有用
https://my.oschina.net/u/4553401/blog/4727173