(Xviii) golang - defer keyword

In the function, programmers often need to create a resource (for example, database connections, file handles, locks, etc.), in order to function after the implementation is complete, timely release of resources, go designer provides defer (delay mechanism)

 With the stated defer sentence will not be executed immediately, but is stored to defer the stack, after the program is finished, then remove the (last out) from the stack:

 

 Note that, if the present value of the defer, the corresponding value will be copied with the stack:

 

 The main value is to defer When the function is finished, the timely release of resources function to create:

 

Guess you like

Origin www.cnblogs.com/xiximayou/p/11889450.html