Created
April 10, 2019 00:46
-
-
Save tevin-morake/fc84d815ca5a8d6863b3b8bd756c8928 to your computer and use it in GitHub Desktop.
Just explaining closure in go
This file contains 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" | |
) | |
/* | |
* What is Closure in go ? | |
* Closure is basically one scope enclosing other scopes . | |
* What does that mean ? | |
* It means that variables declared in the outer scope are accessible in inner scopes | |
*/ | |
func main() { | |
math_multi := outer() | |
fmt.Printf("The multiplication result of allowing the inner function having access to the outer functions scope is : %d", math_multi()) | |
fmt.Println("\n", "===\t", "\t", "\t", "\t", "This my dear friend's, is closure", "\t", "\t", "\t", "\t ===") | |
} | |
func outer() func() int { | |
a := 10 | |
return func() int { | |
b := 2 | |
return a * b | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment