当前位置:网站首页>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
边栏推荐
- [MCU] 51 MCU burning those things
- 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
- Pychart automatically enters the test mode when running the program
- Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
- Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
- leetcode-6131:不可能得到的最短骰子序列
- Huatai Securities account opening process, is it safe to open an account on your mobile phone
- Today's sleep quality record 75 points
- Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
猜你喜欢

Matlab---eeglab check EEG signal
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/60/09c5f44d64b96c6e4d57e5f426e4ed.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results

FanoutExchange交换机代码教程

Leetcode-6131: the shortest dice sequence impossible to get

Add startup software items when the win system starts up

Basic knowledge of Marine Geology

Decompile app

Key network protocols in tcp/ip four layer model

两数,三数之和

Too many passwords, don't know how to record? Why don't you write a password box applet yourself
随机推荐
作为测试,如何理解线程同步异步
[MCU] 51 MCU burning those things
Leetcode-919: complete binary tree inserter
结构体,枚举类型与联合体
Kubernetes advanced part learning notes
476-82(322、64、2、46、62、114)
[tensorrt] trtexec tool to engine
Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
Embedded development: embedded foundation -- threads and tasks
Focus on data | Haitai Fangyuan directly hits the construction idea of data security governance in the securities industry
一道golang中关于接口和实现的面试题
基于腾讯地图实现精准定位,实现微信小程序考勤打卡功能
Use of C log4net: add file name and line number to the output log content; Repackaged class output file name and line number
Open source SPL enhances mangodb computing
Oracle views the SQL statements with the slowest execution and the most queries
Remote - actual combat
[FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes
seven point two three
Remote - basic principle introduction
LeetCode通关:哈希表六连,这个还真有点简单