当前位置:网站首页>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
边栏推荐
- 有哪些优化mysql索引的方式请举例(sqlserver索引优化)
- Leetcode customs clearance: hash table six, this is really a little simple
- If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
- Focus on data | Haitai Fangyuan directly hits the construction idea of data security governance in the securities industry
- Automated testing ----- selenium (I)
- Jmeter分布式压测
- Online random coin tossing positive and negative statistical tool
- [matlab] download originality documents based on oil monkey script and MATLAB
- 一道golang中defer和函数结合的面试题
- Remote—基本原理介绍
猜你喜欢

The database empties the table data and makes the primary key start from 1

【TensorRT】动态batch进行推理

Kubernetes进阶部分学习笔记

leetcode-6131:不可能得到的最短骰子序列
![[tensorrt] dynamic batch reasoning](/img/59/42ed0074de7162887bfe2c81891e20.png)
[tensorrt] dynamic batch reasoning

leetcode-155:最小栈

preprocessor directives

Airtest解决“自动装包”过程中需要输入密码的问题(同适用于随机弹框处理)

Canvas fill gradient

Leetcode skimming -- guess the size of numbers II 375 medium
随机推荐
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
day04_ array
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
leetcode-6127:优质数对的数目
leetcode-6125:相等行列对
Niuke-top101-bm37
[workplace rules] it workplace rules | poor performance
LeetCode刷题——猜数字大小II#375#Medium
租房二三事
The database empties the table data and makes the primary key start from 1
[technical dry goods] how to ensure the idempotency of the interface?
Clickhouse notes 02 -- installation test clickvisual
Introduction to MySQL engine and InnoDB logical storage structure
一道golang中关于接口和实现的面试题
[paper reading] unpaired image to image translation using cycle consistent advantageous networks
Cesium polygon gradient texture (canvas)
Oracle views the SQL statements with the slowest execution and the most queries
Has baozi ever played in the multi merchant system?
[online tutorial] iptables official tutorial -- learning notes 2
Brush questions with binary tree (4)