当前位置:网站首页>Golang defer
Golang defer
2022-07-04 09:32:00 【动态一时爽,重构火葬场】
在Golang使用defer常常会迷惑于以下两个问题
defer关键字的调用时机以及多次调用defer时执行顺序是如何确定的;defer关键字使用传值的方式传递参数时会进行预计算,导致不符合预期的结果;
defer的调用时机是在当前函数返回之前调用的,执行顺序是先入后出。
预计算参数
defer也是传参的。比如说以下代码就会输入0,而不是最终的1
n := 0
defer fmt.Println(n)
n = 1
其原因在于defer会立刻拷贝defer函数中引用的外部参数,也就是说运行到defer fmt.Println(n)时,n的值便已经在defer函数里面啦
即使defer的是个有参方法,也会拷贝到参数
func TestDeferPassParmFunc(t *testing.T) { n := 0 defer deferPassParam(n) n = 1 } func deferPassParam(r int) { fmt.Println(r) } // output: // 0
如果想避免这种情况我们可以defer匿名函数或者指针参数函数
func TestDeferPassParmFunc(t *testing.T) {
n := 0
defer deferPassParam(&n)
n = 1
}
func deferPassParam(r *int) {
fmt.Println(*r)
}
// output:
// 1
常见defer现象
连续defer,先入后出
按照先入后出执行
defer fmt.Println("one")
defer fmt.Println("two")
defer fmt.Println("three")
// output:
// three
// two
// one
defer声明,变量确认
变量在defer声明时便已经确认,即使后面变量发生改变,defer实际执行时也与声明时保持一致。
n := 0
defer fmt.Println(n)
n = 1
// output:
// 0
defer闭包,参数可修改
对于有名返回值,被defer的函数是闭包,且该函数为命名返回值,那么defer是可以访问返回值,并修改
func namedReturn() (r int) {
defer func() {
r = 1
}()
return 2
}
// output
// 1
当然,如果被defer的函数不是闭包或匿名,但是只要传入有名返回值的指针,其实还是可以的
func namedReturnWithDeferNamedFunc() (r int) { defer namedFuncForDer(&r) return 2 } func namedFuncForDer(r *int) { *r = 1 } // output // 1
除了命名返回值这种特殊参数外,其他普通参数其他也可访问修改
// 无参匿名
n := 1
defer func() {
fmt.Println(n)
}()
n = n + 1
return n
// output:
// 2
// 有参匿名
n := 1
defer func(x int) {
fmt.Println(n)
}(n)
n = n + 1
return n
// output:
// 2
总结
- defer按照栈顺序,先定义的后执行
- defer闭包会在defer执行时,获取或改变闭包内变量(但不改变非命名返回值)
- defer匿名函数也会获取或者改变闭包内变量(但不改变非命名返回值)
- defer普通函数,在非指针参数情况下,与defer声明时变量值一致
Ref
- https://draveness.me/golang/docs/part2-foundation/ch05-keyword/golang-defer
边栏推荐
- Awk from getting started to digging in (11) detailed explanation of awk getline function
- Talk about single case mode
- How should PMP learning ideas be realized?
- Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
- Latex download installation record
- Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
- 2022-2028 research and trend analysis report on the global edible essence industry
- Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
- Four common methods of copying object attributes (summarize the highest efficiency)
- Research Report on the development trend and Prospect of global and Chinese zinc antimonide market Ⓚ 2022 ~ 2027
猜你喜欢

If you can quickly generate a dictionary from two lists

HMS core helps baby bus show high-quality children's digital content to global developers

2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors

Clion console output Chinese garbled code

How do microservices aggregate API documents? This wave of show~

AMLOGIC gsensor debugging
![C language - Introduction - Foundation - syntax - [operators, type conversion] (6)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)

Implementation principle of redis string and sorted set

2022-2028 global elastic strain sensor industry research and trend analysis report

LeetCode 74. Search 2D matrix
随机推荐
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 74. Search 2D matrix
Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
After unplugging the network cable, does the original TCP connection still exist?
2022-2028 global visual quality analyzer industry research and trend analysis report
Talk about single case mode
2022-2028 global special starch industry research and trend analysis report
HMS core helps baby bus show high-quality children's digital content to global developers
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
什么是uid?什么是Auth?什么是验证器?
How to ensure the uniqueness of ID in distributed environment
Write a jison parser from scratch (3/10): a good beginning is half the success -- "politics" (Aristotle)
2022-2028 research and trend analysis report on the global edible essence industry
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
2022-2028 global small batch batch batch furnace industry research and trend analysis report
Rules for using init in golang