diff --git a/sample.go b/sample.go new file mode 100644 index 0000000..19e4721 --- /dev/null +++ b/sample.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +// fib returns a function that returns +// successive Fibonacci numbers. +func fib() func() int { + a, b := 0, 1 + return func() int { + a, b = b, a+b + return a + } +} + +func main() { + f := fib() + // Function calls are evaluated left-to-right. + fmt.Println(f(), f(), f(), f(), f()) +}