Implementing a Lazy Generator

This lesson gives detailed knowledge on generators and lazy evaluation. It explains how to write a generator for lazy evaluation.

We'll cover the following


A generator is a function that returns the next value in a sequence each time the function is called, like:

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

It is a producer that only returns the next value, not the entire sequence. This is called _lazy evaluations, which means only computing what you need at the moment, therefore saving valuable resources (memory and CPU). It is technology for the evaluation of expressions on demand.


An example (of lazy generation) would be the generation of an endless sequence of even numbers. To generate it and use those numbers one by one would be difficult and certainly would not fit into memory! But, a simple function per type with a channel and a goroutine can do the job.

For example, in the following program, we see a Go implementation with a channel of a generator of ints. The channel is named yield and resume, terms commonly used in coroutine code.

Get hands-on with 1200+ tech skills courses.