Skip to content

Instantly share code, notes, and snippets.

@holys
Last active August 29, 2015 14:06
Show Gist options
  • Save holys/e0752834f90af2e47179 to your computer and use it in GitHub Desktop.
Save holys/e0752834f90af2e47179 to your computer and use it in GitHub Desktop.
//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