Go language defer you don't know the secret!

Source: Internet
Author: User

The defer function of Go language is very powerful, it is very convenient for resource management, but if it doesn't work well, there will be a trap. Let's look at some examples first.

Example one: Defer is advanced after the

This is natural, the subsequent statements will depend on the previous resources, so if the preceding resources are released first, the subsequent statements will not be able to play.

1 Func Main () {2     var whatever [5]struct{}34for      i: =  Range whatever {5        defer FMT. Println (i)6    }7 }

This output should be obvious, that is 4 3 2 1 0

Example two: Defer encounter closures

Func Main () {    var whatever [5]struct{}    for i: =  Range whatever {        defer func () {fmt. Println (i)} ()}    }

This output may exceed some people's expectations, as a result 4 4 4 4 4

In fact go is very clear, let's see how go spec says

Each time a "defer" statement executes, the function value and parameters to the call is evaluated as Usualand saved anew But the actual function was not invoked.

That is, the function is executed normally, since the variable i is in the execution time has become 4, so the output is all 4.

Example three: Defer F.close

This is very often used by everyone, but go programming is an example of a mistake that might be accidentally made.

Type Teststruct{Namestring}func (t*Test) Close () {fmt. Println (T.name,"closed");} Func main () {TS:=[]test{{"a"},{"b"},{"C"}}     for_,t: =Range ts{defer T.close ()}}

This output does not look like our expected output C B A, but output C c c

But according to the previous go spec in the instructions, should be output C B a only right AH.

Let's call it in a different way.

Example four: A call like example one

Type Teststruct{Namestring}func (t*Test) Close () {fmt. Println (T.name,"closed");} Func Close (t Test) {t.close ()}func main () {TS:=[]test{{"a"},{"b"},{"C"}}     for_,t: =range ts{Close (t)}}

This time the output is C B a

Of course, if you do not want to write a function, it is also very simple, can be like the following, the same output C B a

Example five: A statement that looks superfluous

Type Teststruct{Namestring}func (t*Test) Close () {fmt. Println (T.name,"closed");} Func main () {TS:=[]test{{"a"},{"b"},{"C"}}     for_,t: =range ts{T2:=T T2. Close ()}}

Through the above example, combined

Each time a "defer" statement executes, the function value and parameters to the call is evaluated as Usualand saved anew But the actual function was not invoked.

This sentence. The following conclusions can be drawn:

Defer after the statement is executed, the parameters of the function call are saved, but not executed. That is, a copy. But it doesn't say how the this pointer is handled here, This example shows that the go language does not treat this explicitly written pointer as a parameter.

Go language defer you don't know the secret!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.