Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GeneratorVariadicFunc
deprecated
func GeneratorVariadicFunc(f func(yield Yield, args ...interface{})) func(args ...interface{}) *Generator
Deprecated: Use GeneratorFunc wrapped by closure instead.
Example ¶
package main
import (
"fmt"
"github.com/searKing/golang/go/go/generator"
)
func main() {
g := func(i int) *generator.Generator {
g := generator.GeneratorVariadicFunc(func(yield generator.Yield, args ...interface{}) {
i := (args[0]).(int)
if !yield(i) {
return
}
if !yield(i + 10) {
return
}
})
gen := g(i)
return gen
}
gen := g(10)
for msg := range gen.C {
fmt.Println(msg)
}
}
Output: 10 20
Types ¶
type Generator ¶
type Generator struct {
// Used by Next, to notify or deliver what is generated, as next in python or ES6
C <-chan interface{}
// contains filtered or unexported fields
}
Generator behaves like Generator in python or ES6 Generator function contains one or more yield statement. Generator functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop. see https://wiki.python.org/moin/Generators see https://www.programiz.com/python-programming/generator see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
func GeneratorFunc ¶
GeneratorFunc returns an object (iterator) which we can iterate over (one value at a time). It returns a Generator that can be used to cancel the call using its Stop method. Iterate will be stopped when f is return or Stop is called.
Example ¶
package main
import (
"fmt"
"github.com/searKing/golang/go/go/generator"
)
func main() {
g := func(i int) *generator.Generator {
return generator.GeneratorFunc(func(yield generator.Yield) {
if !yield(i) {
return
}
if !yield(i + 10) {
return
}
})
}
gen := g(10)
for msg := range gen.C {
fmt.Println(msg)
}
}
Output: 10 20
func GeneratorFuncWithSupplier ¶
func GeneratorFuncWithSupplier(supplierC <-chan interface{}, f func(msg interface{})) *Generator
GeneratorFuncWithSupplier waits for the supplierC to supply and then calls f in its own goroutine every time. It returns a Generator that can be used to cancel the call using its Stop method. Consume will be stopped when supplierC is closed.
func GeneratorWithSupplier ¶
func GeneratorWithSupplier(supplierC <-chan interface{}) *Generator
GeneratorWithSupplier is like GeneratorFunc. But it's data src is from supplierC. Iterate will be stopped when supplierC is closed or Stop is called.
Example ¶
package main
import (
"fmt"
"github.com/searKing/golang/go/go/generator"
)
func main() {
var g *generator.Generator
supplierC := make(chan interface{})
supplierF := func(i int) {
consumer := g.Yield(supplierC)
if !consumer(i) {
return
}
if !consumer(i + 10) {
return
}
close(supplierC)
}
g = generator.GeneratorWithSupplier(supplierC)
go supplierF(10)
for msg := range g.C {
fmt.Println(msg)
}
}
Output: 10 20
func (*Generator) Next ¶
Next behaves like an iterator, i.e. it can be used in a for loop. It's a grammar sugar for chan
Example ¶
package main
import (
"fmt"
"github.com/searKing/golang/go/go/generator"
)
func main() {
g := func(i int) *generator.Generator {
return generator.GeneratorFunc(func(yield generator.Yield) {
if !yield(i) {
return
}
if !yield(i + 10) {
return
}
})
}
gen := g(10)
for {
msg, ok := gen.Next()
if !ok {
return
}
fmt.Println(msg)
}
}
Output: 10 20
func (*Generator) Stop ¶
Stop prevents the Generator from firing, with the channel drained. Stop ensures the channel is empty after a call to Stop. It returns true if the call stops the generator, false if the generator has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.
This cannot be done concurrent to other receives from the Generator's channel.
For a generator created with GeneratorFuncWithSupplier(supplierC, f), if t.Stop returns false, then the generator has already expired and the function f has been started in its own goroutine; Stop does not wait for f to complete before returning. If the caller needs to know whether f is completed, it must coordinate with f explicitly.
func (*Generator) Yield ¶
Yield is a grammar sugar for data src of generator ok returns true if msg sent; false if consume canceled If a function contains at least one yield statement (it may contain other yield or return statements), it becomes a generator function. Both yield and return will return some value from a function. The difference is that, while a return statement terminates a function entirely, yield statement pauses the function saving all its states and later continues from there on successive calls.