Skip to content

Instantly share code, notes, and snippets.

@tevin-morake
Created April 10, 2019 00:46
Show Gist options
  • Save tevin-morake/fc84d815ca5a8d6863b3b8bd756c8928 to your computer and use it in GitHub Desktop.
Save tevin-morake/fc84d815ca5a8d6863b3b8bd756c8928 to your computer and use it in GitHub Desktop.
Just explaining closure in go
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