当前位置:网站首页>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
边栏推荐
- Virtual memory management
- [untitled]
- PHP解决并发问题的几种实现
- [cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
- [vscode] support argparser/ accept command line parameters
- 【Cadence Allegro PCB设计】error: Possible pin type conflict GND/VCC Power Connected to Output
- 多项式相加
- 【硬件工程师】关于信号电平驱动能力
- Redis源码与设计剖析 -- 16.AOF持久化机制
- Excel表格 / WPS表格中怎么在下拉滚动时让第一行标题固定住?
猜你喜欢
随机推荐
Google Earth engine - download the globalmlbuildingfootprints vector collection of global buildings
超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!
[vscode] support argparser/ accept command line parameters
go语言context控制函数执行超时返回
Redis cluster deployment based on redis6.2.4
我也是醉了,Eureka 延迟注册还有这个坑!
03. Longest substring without repeated characters
Notes on Flickr's dataset
【Cadence Allegro PCB设计】永久修改快捷键(自定义)~亲测有效~
Update 3dcat real time cloud rendering V2.1.2 release
ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
枚举类和魔术值
Add batch delete
如何看一本书
Lvgl 7.11 tileview interface cycle switching
Which one of the electronic products has a longer service life??
Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes
Pymongo saves data in dataframe format (insert_one, insert_many, multi-threaded saving)
Is it safe to open a futures account online? How to apply for a low handling fee?
Crawler framework crawler









