Skip to content

Instantly share code, notes, and snippets.

@ilius
Last active February 15, 2024 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilius/c144020a1b6afdc1c66ddde87cdd4aa8 to your computer and use it in GitHub Desktop.
Save ilius/c144020a1b6afdc1c66ddde87cdd4aa8 to your computer and use it in GitHub Desktop.
Go generators
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
import "io"
type Gen interface {
Next(args any...) (any, error) // EOF
End() bool
}
type IterationGen struct {
end int
step int
cur int
}
func (g *IterationGen) Next(args any...) (any, error) {
if g.cur >= g.end {
return nil, io.EOF
}
cur := g.cur
g.cur += g.step
return g.cur, nil
}
func (g *IterationGen) End() bool {
ruturn g.cur >= g.end
}
func NewIterator(start int, end int, step int) *IterationGen {
return &IterationGen{
cur: start,
step: step,
end: end,
}
}
func main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment