A generator is a function that returns the next value of a sequence each time it is called. The biggest advantage of using generators is the lazy creation of new values of the sequence. In Go, this can either be expressed with an interface or with a channel. One of the upsides of generators when they're used with channels is that they produce values concurrently, in a separate goroutine, leaving the main goroutine capable of executing other kinds of operations on them.
It can be abstracted with a very simple interface:
type Generator interface {
Next() interface{}
}
type GenInt64 interface {
Next() int64
}
The return type of the interface will depend on the use case, which is int64 in our case. The basic implementation of it could be something such as a simple counter:
type genInt64 int64
func (g *genInt64) Next() int64 {
*g++
return...