当前位置:网站首页>Go defer and recover simple notes
Go defer and recover simple notes
2022-07-25 17:39:00 【kankan231】
defer Mechanism
go Of defer The mechanism is used to release some resources before the end of the function , Multiple... Can be defined in the function defer, After definition, execute first , Advanced chef , It's a stack . When the function wants to return when , I will put return Copy the value of , And then do it one by one defer Code ( stay defer Change in return Variables of no longer affect return Value ), Finally, return to the copied return value
recover Mechanism
recover Used to capture the occurrence of the current function and its called sub functions panic error , Let the current program continue , If the whole program is not captured, it will exit .recover Only written in defer Only valid in code blocks
The Works of Liezi :
package main
import "fmt"
func test1() int {
defer func() {
err:=recover()
if err != nil {
fmt.Println("panic:",err)
}
}()
test2()
fmt.Println(" You can't see me ")
return 1
}
func test2() {
var p *int
*p = 1//runtime panic
}
func main() {
fmt.Println(" Before execution ")
ret:=test1()
fmt.Println("return:",ret)
fmt.Println(" After execution ")
}Execution results :
Before execution
panic: runtime error: invalid memory address or nil pointer dereference
return: 0
After execution analysis : According to the running results of the program , Happen when panic, Later code will no longer be executed , until recover after . In this column test1() Caught in panic,test1 The following code is no longer executed ,test1() Will end and return a zero value of the return value type ,test1 The return value type of is int, So back 0, go back to main Continue execution in function
边栏推荐
- 我也是醉了,Eureka 延迟注册还有这个坑!
- 面试官:说说 log.Fatal 和 panic 的区别
- [Hardware Engineer] can't select components?
- Crawler framework crawler
- go channel简单笔记
- 8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
- Methods of de duplication and connection query in MySQL database
- Automated test Po design model
- Virtual memory management
- stm32F407------SPI
猜你喜欢

WPF implements user avatar selector

生成扩散模型漫谈:DDPM = 贝叶斯 + 去噪

哈夫曼树的构建

计算日期或日期格式化
P2P 之 UDP穿透NAT的原理与实现

精彩记录

stm32F407------SPI

Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant

ACL 2022 | 基于最优传输的对比学习实现可解释的语义文本相似性

Take you to a preliminary understanding of multiparty secure computing (MPC)
随机推荐
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
Google Earth engine - download the globalmlbuildingfootprints vector collection of global buildings
ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
[Hardware Engineer] can't select components?
Pymongo saves data in dataframe format (insert_one, insert_many, multi-threaded saving)
stm32F407------SPI
go defer与recover简单笔记
HCIP第一天实验
We were tossed all night by a Kong performance bug
我想理财,不懂,有没有保本金的理财产品?
PostgreSQL里有只编译语句但不执行的方法吗?
霸榜COCO!DINO: 让目标检测拥抱Transformer
服务器端架构设计期末复习知识点总结
【PHP伪协议】源码读取、文件读写、任意php命令执行
如何看一本书
Take you to a preliminary understanding of multiparty secure computing (MPC)
How to rectify the unqualified EMC of electronic products?
新版selenium4.3在egde浏览器的无头模式
博后招募 | 西湖大学机器智能实验室招聘博士后/助理研究员/科研助理
Does PgSQL have a useful graphical management tool?