Coroutines

Wikipedia: Coroutine
The coro module provides cooperative coroutines. A coroutine suspends at each yield call and resumes when iterated or when .resume() is called.

fmt := import("fmt")
coro := import("coro")
countdown := coro.new(func(yield) {
    for i := 5; i > 0; i-- {
        yield(i)
    }
})

v, ok := countdown.resume()
fmt.printf("%v %v\n", v, ok)
v, ok = countdown.resume()
fmt.printf("%v %v\n", v, ok)

try it

1
2
3
5 true
4 true
loading…