Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages