-
-
Save holys/e0752834f90af2e47179 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//refer: http://localhost:6060/blog/defer-panic-and-recover | |
package main | |
import ( | |
"fmt" | |
) | |
//1. A deferred function's arguments are evaluated when the defer statement is evaluated. | |
func a() { | |
i := 0 | |
defer fmt.Println(i) | |
i++ | |
return | |
} | |
//2. Deferred function calls are executed in Last In First Out order after_the surrounding function returns. | |
func b() { | |
for i := 0; i < 4; i++ { | |
defer fmt.Println(i) | |
} | |
} | |
//3. Deferred functions may read and assign to the returning function's named return values. | |
func c() (i int) { | |
defer func() { i++ }() | |
return 1 | |
} | |
func main() { | |
a() | |
b() | |
fmt.Println(c()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment