# 14.8 惰性生成器的实现

``````    generateInteger() => 0
generateInteger() => 1
generateInteger() => 2
....``````

``````package main

import (
"fmt"
)

var resume chan int

func integers() chan int {
yield := make(chan int)
count := 0
go func() {
for {
yield <- count
count++
}
}()
return yield
}

func generateInteger() int {
return <-resume
}

func main() {
resume = integers()
fmt.Println(generateInteger())  //=> 0
fmt.Println(generateInteger())  //=> 1
fmt.Println(generateInteger())  //=> 2
}``````

``````package main

import (
"fmt"
)

type Any interface{}
type EvalFunc func(Any) (Any, Any)

func main() {
evenFunc := func(state Any) (Any, Any) {
os := state.(int)
ns := os + 2
return os, ns
}

even := BuildLazyIntEvaluator(evenFunc, 0)

for i := 0; i < 10; i++ {
fmt.Printf("%vth even: %v\n", i, even())
}
}

func BuildLazyEvaluator(evalFunc EvalFunc, initState Any) func() Any {
retValChan := make(chan Any)
loopFunc := func() {
var actState Any = initState
var retVal Any
for {
retVal, actState = evalFunc(actState)
retValChan <- retVal
}
}
retFunc := func() Any {
return <- retValChan
}
go loopFunc()
return retFunc
}

func BuildLazyIntEvaluator(evalFunc EvalFunc, initState Any) func() int {
ef := BuildLazyEvaluator(evalFunc, initState)
return func() int {
return ef().(int)
}
}``````

``````0th even: 0
1th even: 2
2th even: 4
3th even: 6
4th even: 8
5th even: 10
6th even: 12
7th even: 14
8th even: 16
9th even: 18``````