Preface
defer is a keyword in the golang language. It is used for the release of resources and will be called before the function returns.
Generally, the following mode is adopted:
f,err := (filename) if err != nil { panic(err) } defer ()
If there are multiple defer expressions, the order of call is similar to the stack, the later the defer expression is called first.
The syntax for delay calling functions is as follows:
defer func_name(param-list)
When a function calls the keyword defer, the execution of the function will be postponed until the function containing the defer statement will be executed before it will be returned. For example:
func main() { defer ("Fourth") ("First") ("Third") }
The final printing order is as follows:
First Second Third
It should be noted that the value of the function parameter called by defer is determined when defer is defined.
For example:
i := 1 defer ("Deferred print:", i) i++ ("Normal print:", i)
The printed content is as follows:
Normal print: 2 Deferred print: 1
So we know, "defer ("Deferred print:", i)"
When called, the value of i has been determined, so it is equivalent todefer ("Deferred print:", 1)
It's right.
When it needs to be emphasized, the value of the function parameter called by defer is determined when defer is defined, and the value of the variable used inside the defer function needs to be determined when the function is run.
For example:
func f1() (r int) { r = 1 defer func() { r++ (r) }() r = 2 return } func main() { f1() }
In the above example, the final printed content is "3", because after the "r = 2" assignment, the defer function is executed, so in this function, the value of r is 2, and it becomes 3 after increasing.
defer order
If there are multiple defer calls, the order of calls is first in and then out, similar to entering and exiting the stack:
func main() { defer (1) defer (2) defer (3) defer (4) }
The first thing to do is "(4)"
, and then"(3)"
And so on, the final output is as follows:
4 3 2 1
defer
The execution time of the defer function call is after the outer function sets the return value and before it is about to return.
For example:
func f1() (r int) { defer func() { r++ }() return 0 } func main() { (f1()) }
above(f1())
What is printed? Many friends may think that the printed one is 0, but the correct answer is 1. Why is this?
To understand this problem, we need to keep two things in mind
1. The execution time of the defer function call is after the outer function sets the return value, and before it is about to return.
2. The return XXX operation is not atomic.
We will rewrite the above example and you will understand it very well:
func f1() (r int) { defer func() { r++ }() r = 0 return }
The defer function is called after the assignment operation "r = 0", and the return statement is finally the return statement.
Therefore, the above code is equivalent to:
func f1() (r int) { r = 0 func() { r++ }() return }
Next, let's take a look at a more interesting example:
func double(x int) int { return x + x } func triple(x int) (r int) { defer func() { r += x }() return double(x) } func main() { (triple(3)) }
If we have understood what is said above, then the triple function is easy to understand, and it is actually:
func triple(x int) (r int) { r = double(x) func() { r += x }() return }
Defer expression usage scenario
defer is usually used for paired operations such as open/close, connect/disconnect, lock/unlock, etc. to ensure that resources are correctly released under any circumstances. From this perspective, defer operations are similar to the try ... finally statement block in Java.
For example:
var mutex var count = 0 func increment() { () defer () count++ }
existincrement
In the function, in order to avoid the occurrence of race conditions, we use Mutex to lock. However, when performing concurrent programming, locking but forgetting (or unlock is not executed in some cases), often causes catastrophic consequences. In order to ensure that the corresponding unlocking operation is performed after the locking operation, we can use defer to call the unlocking operation.
Summarize
The above is the entire content of this article, and I hope it will be of some help to everyone's study or work. If you have any questions, please leave a message to communicate.