当前位置:网站首页>An interview question combining defer and function in golang
An interview question combining defer and function in golang
2022-07-25 20:54:00 【youngqqcn】
What does the following code output ?
package main
import "fmt"
// What does the following code output ?
func calc(index string, a, b int) int {
ret := a + b
fmt.Printf("%v,%v,%v,%v\n", index, a, b, ret)
return ret
}
func main() {
a := 1
b := 2
defer calc("1", a, calc("10", a, b))
a = 0
defer calc("2", a, calc("20", a, b))
b = 1
}
Please think about it. , The answer is below
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
answer
10,1,2,3
20,0,2,2
2,0,2,2
1,1,3,4
Why? ?
because defer Will first calculate the value of ordinary functions , And then pass it in as a parameter defer function
We will solve this problem , Make a little change , as follows ,
package main
import "fmt"
// What does the following code output ?
func calc(index string, a, b *int) *int {
ret := *a + *b
fmt.Printf("%v,%v,%v,%v\n", index, *a, *b, ret)
return &ret
}
func main() {
a := 1
b := 2
defer calc("1", &a, calc("10", &a, &b))
a = 0
defer calc("2", &a, calc("20", &a, &b))
b = 1
}
Please think about it. , The answer is below
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
answer
10,1,2,3
20,0,2,2
2,0,2,2
1,0,3,3
Why? ? Compared with the above question , This question is changed to pointer as defer The parameters of the function , therefore , Unlike value passing
Let's see , The following questions
What is the output of the following code ? Why? ?
package main
import "fmt"
func main() {
{
defer func() {
fmt.Println("A")
}()
}
fmt.Println("B")
}
,
,
,
Output
B
A
What is the output of the following code ? Why? ?
func main() {
func(){
defer func() {
fmt.Println("A")
}()
}()
fmt.Println("B")
}
,
,
,
Output
A
B
defer Function is to register with the current function , The scope is the current function . The first 1 An example is easy to understand .
The first 2 An example , Because it's a closure ,“ The current function ” It's a closure , When the closure is complete ,defer The registered function will be called . therefore , First, the output A
边栏推荐
- 作为测试,如何理解线程同步异步
- Niuke-top101-bm37
- LeetCode刷题——猜数字大小II#375#Medium
- Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
- 租房二三事
- Canvas fill gradient
- Achieve accurate positioning based on Tencent map, and realize the attendance punch function of wechat applet
- [technical dry goods] how to ensure the idempotency of the interface?
- 【FiddlerTX插件】使用Fiddler抓包腾讯课堂视频下载(抓不到包解决方案)
- Golang language quickly get started to comprehensive practical notes (go language, beego framework, high concurrency chat room, crawler)
猜你喜欢

Yolov7 training error indexerror: list index out of range

Embedded development: embedded foundation -- threads and tasks

476-82(322、64、2、46、62、114)

Matlab---eeglab check EEG signal

Leetcode-79: word search

Product principles of non-financial decentralized application

leetcode-6129:全 0 子数组的数目
![[online tutorial] iptables official tutorial -- learning notes 2](/img/7d/5f8328d1b4c8878f17c95d2658d2d6.jpg)
[online tutorial] iptables official tutorial -- learning notes 2

Achieve accurate positioning based on Tencent map, and realize the attendance punch function of wechat applet
![[MCU] 51 MCU burning those things](/img/fa/8f11ef64a18114365c084fff7d39f6.png)
[MCU] 51 MCU burning those things
随机推荐
Cesium 多边形渐变色纹理(Canvas)
【网络教程】IPtables官方教程--学习笔记2
A detailed explanation of SCP command
leetcode-6125:相等行列对
Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
Today's sleep quality record 75 points
leetcode-6127:优质数对的数目
leetcode-6129:全 0 子数组的数目
Detailed explanation of document operation
Using the OAP aspect causes the controller to be called repeatedly
Beisen Holdings' IPO: a total loss of 4.115 billion yuan in three years, and a total of 2.84 billion yuan in the previous nine rounds of financing
[depth] the new LAAS agreement elephant: the key to revitalizing the development of the defi track
[tensorrt] dynamic batch reasoning
[fiddlertx plug-in] use Fiddler to capture the package Tencent classroom video download (unable to capture the package solution)
day04_ array
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
牛客-TOP101-BM37
有哪些优化mysql索引的方式请举例(sqlserver索引优化)
Leetcode-6125: equal row and column pairs
Jmeter分布式压测