Source file tour/solutions/fib.go

     1  //go:build OMIT
     2  
     3  // Copyright 2012 The Go Authors.  All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  import "fmt"
    10  
    11  // fibonacci is a function that returns
    12  // a function that returns an int.
    13  func fibonacci() func() int {
    14  	f, g := 1, 0
    15  	return func() int {
    16  		f, g = g, f+g
    17  		return f
    18  	}
    19  }
    20  
    21  func main() {
    22  	f := fibonacci()
    23  	for i := 0; i < 10; i++ {
    24  		fmt.Println(f())
    25  	}
    26  }
    27  

View as plain text