Skip to content

Instantly share code, notes, and snippets.

@tristanwietsma
Forked from funny-falcon/deferClosure.go
Created September 17, 2013 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristanwietsma/6594638 to your computer and use it in GitHub Desktop.
Save tristanwietsma/6594638 to your computer and use it in GitHub Desktop.
Deferred closure example with a cycle
package main
import "fmt"
func A(x, y int) {
fmt.Println("The deferred A-call", x, y)
}
func B() int {
theAnswer := 42
for y:=1; y < 10; y++ {
defer func() {
fmt.Println("The deferred scope", theAnswer, y)
A(theAnswer, y)
}()
fmt.Println("The exit inner loop scope", theAnswer, y)
}
fmt.Println("The exit function scope", theAnswer)
return theAnswer
}
func main() {
fmt.Println("Back in the main method", B())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment