// An experiment with closures and bignums package main import "fmt" import "math/big" import "os" import "strconv" // fibonacci is a function that returns // a function that returns an *big.Int. func fibonacci() func() *big.Int { fibNum1 := big.NewInt(0) fibNum2 := big.NewInt(1) innerFib := func () *big.Int { sum := fibNum1 sum.Add(fibNum1, fibNum2) fibNum1, fibNum2 = fibNum2, sum return fibNum1 } return innerFib } func main() { nums, err := strconv.Atoi(os.Args[1]) if err != nil { fmt.Println("Failed to convert", os.Args[1], "to int") os.Exit(1) } f := fibonacci() for i := 0; i < nums; i++ { fmt.Println(i + 1, f()) } }