Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of defer in Go language

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you the sample analysis of defer in the Go language, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Defer mechanism in Go language.

Today we take a look at a piece of defer mechanism code:

Func calc (index string, a, b int) int {ret: = a + b fmt.Println (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}

So what do you think is the correct output of this code?

The defer as we know it is called according to the principle of FILO (Fisrt In Last Out). Our analysis according to this rule may lead to the following results:

20 0 2 22 0 2 210 1 2 31 1 3 4

But when we run it, the result is actually this:

10 1 2 320 0 2 22 0 2 21 1 3 4

How did this happen? This brings us to the official interpretation of defer.

The defer statement pushes the function call to the list. The saved call list is executed after the surrounding function returns. Defer is often used to simplify functions that perform various cleanup operations.

Officially, the defer list (which satisfies FILO) will be called when the function containing defer is "returned". But the official one didn't say how to use it. The reality is that defer will save all the current variables when it is used. If there is a function in such a variable, it will first make a function call to get the value, and then save it.

The actual steps for defer are as follows:

1. In the place of the defer expression, runtime.deferproc (size int32, fn * funcval) is called to save the deferred call. Note that the parameters of the delayed call are saved here.

two。 In return, save the returned value first

3. Call runtime.deferreturn in FILO order, that is, defer the call

4. RET instruction

So when you get to the first defer in this article, all the parameters are evaluated and pushed onto the stack. Then at the second defer, all the parameters are calculated and pushed into the stack. Finally, in the return, pop up the second defer and the first defer in turn. In the end, it's what we see.

The above is the example analysis of defer in Go language. have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report