package main
import (
"bufio"
"fmt"
"io"
"strings"
)
type intGen func() int
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)
return strings.NewReader(s).Read(p) //通过这个方法将字符串写入到缓冲区里面去
}
func fibonacci() intGen {
a, b := 0, 1
return func() int {
a, b = b, a+b
return b
}
}
func PrintFibonacci(gen intGen) {
scanner := bufio.NewScanner(gen) //实现了Read方法,也就实现了io.Writer接口
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
func main() {
f := fibonacci()
PrintFibonacci(f)
}