package main import ( "bufio" "fmt" "io" "strings" ) type IntFunc func() int func fibonacci() IntFunc { a, b := 0, 1 return func() int { a, b = b, a+b return a } } func (f IntFunc) Read(b []byte) (i int, err error) { next := f() if next > 10000 { return 0, io.EOF } s := fmt.Sprintf("%d\n", next) return strings.NewReader(s).Read(b) } func printFileContents(reader io.Reader) { scanner := bufio.NewScanner(reader) for scanner.Scan() { fmt.Printf("%s \n", scanner.Text()) } } func main() { f := fibonacci() printFileContents(f) } 为什么形参是一个io.Reader 实参是一个函数 而不报错
pardon110
相关分类