当前位置:网站首页>一道golang中defer和函数结合的面试题
一道golang中defer和函数结合的面试题
2022-07-25 20:48:00 【youngqqcn】
下面代码输出什么?
package main
import "fmt"
// 下面代码输出什么?
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
}
请思考,答案在下文
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
答案
10,1,2,3
20,0,2,2
2,0,2,2
1,1,3,4
为什么?
因为defer会先计算普通函数的值,再作为参数传入defer函数
我们将此题,稍作改变,如下,
package main
import "fmt"
// 下面代码输出什么?
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
}
请思考,答案在下文
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
答案
10,1,2,3
20,0,2,2
2,0,2,2
1,0,3,3
为什么?与上题相比,此题改成了指针作为defer函数的参数,所以,与值传递不同
我们再看看,下面的题目
请问以下代码输出什么?为什么?
package main
import "fmt"
func main() {
{
defer func() {
fmt.Println("A")
}()
}
fmt.Println("B")
}
,
,
,
输出
B
A
请问以下代码输出什么?为什么?
func main() {
func(){
defer func() {
fmt.Println("A")
}()
}()
fmt.Println("B")
}
,
,
,
输出
A
B
defer函数是向当前函数进行注册,作用域是当前函数。第1个例子就好理解了。
第2个例子,因为是个闭包,“当前函数”就是闭包,当闭包完成之后,defer注册的函数就会被调用。所以,先输出A
边栏推荐
- 毕业从事弱电3个月,我为什么会选择转行网络工程师
- Leetcode-6129: number of all 0 subarrays
- 【单细胞高级绘图】07.KEGG富集结果展示
- Increase swap space
- How to use buffer queue to realize high concurrent order business (glory Collection Edition)
- Huatai Securities account opening process, is it safe to open an account on your mobile phone
- Product principles of non-financial decentralized application
- Introduction to MySQL engine and InnoDB logical storage structure
- leetcode-155:最小栈
- [paper reading] unpaired image to image translation using cycle consistent advantageous networks
猜你喜欢

How to choose a microservice registration center?

Key network protocols in tcp/ip four layer model

Pycharm跑程序时自动进入测试模式

Leetcode-79: word search

文件操作详解

Leetcode-6125: equal row and column pairs

Remote monitoring solution of intelligent electronic boundary stake Nature Reserve

【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]

leetcode-6127:优质数对的数目
随机推荐
牛客-TOP101-BM37
leetcode-6127:优质数对的数目
[technical dry goods] how to ensure the idempotency of the interface?
Docker builds redis cluster
Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
Explain the principle of MySQL master-slave replication in detail
KEGG通路的从属/注释信息如何获取
Using the OAP aspect causes the controller to be called repeatedly
476-82(322、64、2、46、62、114)
每条你收藏的资讯背后,都离不开TA
FanoutExchange交换机代码教程
Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
Pycharm跑程序时自动进入测试模式
Has baozi ever played in the multi merchant system?
文件操作详解
Remote—基本原理介绍
476-82(322、64、2、46、62、114)
Remote—实战
Online XML to JSON tool
Remote - actual combat