当前位置:网站首页>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
边栏推荐
- Add startup software items when the win system starts up
- [online tutorial] iptables official tutorial -- learning notes 2
- MySQL inserts three tables with different values. The association condition is the primary foreign key. How about the syntax of the insertion statement?
- [depth] the new LAAS agreement elephant: the key to revitalizing the development of the defi track
- Follow up of Arlo's thinking
- Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
- 牛客-TOP101-BM38
- Using the OAP aspect causes the controller to be called repeatedly
- Fanoutexchange switch code tutorial
- Leetcode-6129: number of all 0 subarrays
猜你喜欢

【网络教程】IPtables官方教程--学习笔记2

Struct, enum type and union

Leetcode customs clearance: hash table six, this is really a little simple
![[MCU] 51 MCU burning those things](/img/fa/8f11ef64a18114365c084fff7d39f6.png)
[MCU] 51 MCU burning those things

Leetcode-6131: the shortest dice sequence impossible to get
![[online tutorial] iptables official tutorial -- learning notes 2](/img/7d/5f8328d1b4c8878f17c95d2658d2d6.jpg)
[online tutorial] iptables official tutorial -- learning notes 2

How to choose a microservice registration center?

If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)

Key network protocols in tcp/ip four layer model

Brush questions with binary tree (4)
随机推荐
作为测试,如何理解线程同步异步
Achieve accurate positioning based on Tencent map, and realize the attendance punch function of wechat applet
Follow up of Arlo's thinking
Force deduction ----- calculate the money of the force deduction bank
Golang language quickly get started to comprehensive practical notes (go language, beego framework, high concurrency chat room, crawler)
Focus on data | Haitai Fangyuan directly hits the construction idea of data security governance in the securities industry
使用oap切面导致controller被重复调用
Step num problem
Brush questions with binary tree (4)
[depth] the new LAAS agreement elephant: the key to revitalizing the development of the defi track
Clickhouse notes 02 -- installation test clickvisual
Kubernetes advanced part learning notes
Jmeter分布式压测
ROS_ Rqt toolbox
wokerman 自定义写入日志文件
[matlab] download originality documents based on oil monkey script and MATLAB
【FiddlerTX插件】使用Fiddler抓包腾讯课堂视频下载(抓不到包解决方案)
Canvas 填充渐变
Huatai Securities account opening process, is it safe to open an account on your mobile phone
Leetcode customs clearance: hash table six, this is really a little simple