package main
import (
"fmt"
"io"
"strings"
"bufio"
)
func fibonacci() intGen{
a, b := 0, 1
return func() int {
a, b = b, a + b
return a
}
}
type intGen func() int
//实现interface
func (g intGen) Read (
p []byte) (n int, err error) {
next := g()
if next > 10000 {
return 0, io.EOF
}
s := fmt.Sprintf("%d\n", next)
// TODO: incorrect if p is too small!
return strings.NewReader(s).Read(p)
}
func printFileContents(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
func main() {
f := fibonacci()
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
printFileContents(f)
}
来源:CSDN
作者:INGNIGHT
链接:https://blog.csdn.net/INGNIGHT/article/details/104149630