Skip to content

Instantly share code, notes, and snippets.

@ernstvanzyl
Last active August 12, 2018 18:39
Show Gist options
  • Select an option

  • Save ernstvanzyl/6ac4232df85f1d01a12a39fb57682a83 to your computer and use it in GitHub Desktop.

Select an option

Save ernstvanzyl/6ac4232df85f1d01a12a39fb57682a83 to your computer and use it in GitHub Desktop.
Defer gotcha
// 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