SHARE
TWEET

Untitled

a guest Jul 15th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. // fibonacci is a function that returns
  6. // a function that returns an int.
  7. func fibonacci() func() int {
  8.     sum := 0
  9.     num1 := 0
  10.     num2 := 1
  11.  
  12.     return func() int {
  13.         sum += num1
  14.         num1 = num2
  15.         num2 = sum
  16.         return sum
  17.     }
  18. }
  19.  
  20. func main() {
  21.     f := fibonacci()
  22.     for i := 0; i < 10; i++ {
  23.         fmt.Println(f())
  24.     }
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top