Last active
August 12, 2018 18:39
-
-
Save ernstvanzyl/6ac4232df85f1d01a12a39fb57682a83 to your computer and use it in GitHub Desktop.
Defer gotcha
This file contains hidden or 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
| // Defer gotchas | |
| /* Output: | |
| Hello, playground | |
| Defer 3 2 | |
| Defer 2 1 | |
| Defer 1 2 | |
| */ | |
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| func showInt(msg string, n *int) { | |
| fmt.Println(msg, *n) | |
| } | |
| func main() { | |
| i := 1 | |
| ptr := &i | |
| defer func(){fmt.Println("Defer 1", i)}() | |
| defer fmt.Println("Defer 2", i) | |
| defer showInt("Defer 3", ptr) | |
| i=2 | |
| ptr=nil | |
| fmt.Println("Hello, playground") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment