FibonacciClosure.go 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 Show hidden characters package main import "fmt" // fibonacci is a function that returns // a function that returns an int. func fibonacci () func () int { a , b := 0 , 1 return func () int { a , b = b , a + b //This works because first RHS is computed and stored and then assigned to LHS, //'a' doesn't changes value before b = a+b return b - a } } func main () { f := fibonacci () for i := 0 ; i < 10 ; i ++ { fmt . Println ( f ()) } } /* OUTPUT: 0 1 1 2 3 5 8 13 21 34 */