Created
March 31, 2016 10:31
-
-
Save keshavab/8f4195dc68bb3200ef3c3172976bb3aa to your computer and use it in GitHub Desktop.
closure introduction
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
| package main | |
| import "fmt" | |
| func outer(name string) { | |
| // variable in outer function | |
| text := "Modified " + name | |
| // foo is a inner function and has access to text variable, is a closure | |
| // closures have access to variables even after exiting this block | |
| foo := func() { | |
| fmt.Println(text) | |
| } | |
| // calling the closure | |
| foo() | |
| } | |
| func main() { | |
| outer("hello") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment