当前位置:网站首页>一道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
边栏推荐
- Vulnhub | dc: 5 | [actual combat]
- Remote—基本原理介绍
- 程序的编译和运行
- Unity vs -- the default debugging in VS is to start rather than attach to unity debugging
- Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
- Leetcode-6129: number of all 0 subarrays
- Today's sleep quality record 75 points
- Kubernetes进阶部分学习笔记
- The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
- 预处理指令
猜你喜欢

预处理指令

IEC61131 address representation

Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)

Leetcode customs clearance: hash table six, this is really a little simple

程序的编译和运行

leetcode-919:完全二叉树插入器

火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布

Step num problem

Docker builds redis cluster

Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
随机推荐
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!
FanoutExchange交换机代码教程
Matlab---eeglab check EEG signal
Wokerman custom write log file
Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
预处理指令
leetcode-114:二叉树展开为链表
Remote - actual combat
数据库清空表数据并让主键从1开始
KEGG通路的从属/注释信息如何获取
Leetcode-79: word search
[cloud native] use of Nacos taskmanager task management
Online XML to JSON tool
[depth] the new LAAS agreement elephant: the key to revitalizing the development of the defi track
The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
Niuke-top101-bm37
Pycharm跑程序时自动进入测试模式
Increase swap space
Golang language quickly get started to comprehensive practical notes (go language, beego framework, high concurrency chat room, crawler)
Hello, I'd like to ask questions about C and database operation.