The go language is very flexible, as long as the object implements the appropriate method can be seen as implementing an interface, similar to the Durk Type,
To implement the Read method for Fibonacci, you can read the next Fibonacci value as if you were reading a file.
Example code:
Ackage MainImport ( "FMT" "io" "Bufio" "Strings" "StrConv") func Fibonacci () Intgen {//Fibonacci sequence, return a intgen type A, B:= 0, 1returnfunc () int {A, B= B, A +breturnA}} Type Intgen func () int//defines a Func type that returns the Int type func (g Intgen) Read (P []byte) (n int, err error) {//implement the Read method for Intgen so that the Printfilecontents function can manipulate next as it reads a file:=g ()ifNext > 100000 { return0, Io. EOF}S: = Fmt. Sprintf ("%d\n", Next) s:= StrConv. Itoa (Next) +"\ n" returnStrings. Newreader (s). Read (p)//Use the strings Newreader method to implement the Read interface}func printfilecontents (reader io. Reader) {//read content from reader scanner:=Bufio. Newscanner (reader) forscanner. Scan () {fmt. PRINTLN (scanner. Text ())}}func main () {f:=Fibonacci () printfilecontents (f)}
The Go language implements the Read method for the Fibonacci function