go的defer


// defer only on the internal function
// defer to delay calling in Verge before the end of the function it calls
multiple defer a function at the same time, the execution order: last in, first out . No matter what error occurs even though the collapse sequence, front defer can be executed
// the defer only on the internal functions
 // the defer delay call, the call before the end of the main function 
FUNC Test (Int32 X) { 
    fmt.Println ( " Print: " , 100 / X) 
} 

FUNC TextDefer () { 
    the defer FMT. println ( " statement is executed after the collapse of the program " ) 
    fmt.Println ( " statement of program execution order " ) 
    the Test ( 0 ) 
    the defer fmt.Println ( " after the collapse of the program can come to you? can not " ) 
    fmt.Println ( " statements can not be executed in program order " ) 

}

// the defer only on the internal functions
 // the defer delay call, the call before the end of the main function 
FUNC Test (Int32 X) { 
    fmt.Println ( " Print: " , 100 / X) 
} 

FUNC TextDefer () { 
    the defer FMT. println ( " statement is executed after the collapse of the program " ) 
    fmt.Println ( " statement of program execution order " ) 
    the defer the Test ( 0 ) // Add defer, then all will be executing 
    the defer fmt.Println ( " after the collapse of the program but also to do? can not " ) 
    fmt.Println ( " statement can not be executed in program order " ) 
}

1, defer binding and anonymous functions

 

Guess you like

Origin www.cnblogs.com/oceanran/p/12613831.html