Skip to content
/ coro Public

Cooperative coroutines on top of goroutines.

License

Notifications You must be signed in to change notification settings

tcard/coro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

coro Actions Go Reference

Package coro implements cooperative coroutines on top of goroutines.

It then implements generators on top of that.

resume := coro.New(ctx, func(yield func()) {
	for i := 1; i <= 3; i   {
		fmt.Println("coroutine:", i)
		yield()
	}
	fmt.Println("coroutine: done")
})

fmt.Println("not started yet")
for resume() {
	fmt.Println("yielded")
}
fmt.Println("returned")

// Output:
// not started yet
// coroutine: 1
// yielded
// coroutine: 2
// yielded
// coroutine: 3
// yielded
// coroutine: done
// returned
next := Generate(ctx, func(yield func(string)) error {
	for _, foo := range []string{"foo", "bar", "baz"} {
		yield(foo)
	}
	return errors.New("done")
})

var i int
var err error
for next(&err, &i) {
	fmt.Println("yielded:", i)
}
fmt.Println("returned:", err)

// Output:
// yielded: foo
// yielded: bar
// yielded: baz
// returned: done

About

Cooperative coroutines on top of goroutines.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages