This is a creation in Article, where the information may have evolved or changed.
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.