A tiny lazy future/promise for Go. New starts a Worker in its own
goroutine right away and returns a Responder you can call later to block
for the result.
go get github.com/gophernment/eggpackage main
import (
"errors"
"fmt"
"github.com/gophernment/egg"
)
func main() {
r := egg.New(func() any {
return "hello"
})
fmt.Println(<-r()) // "hello"
// If the Worker panics, the channel receives an error instead of
// crashing the program.
r = egg.New(func() any {
panic(errors.New("boom"))
})
if err, ok := (<-r()).(error); ok {
fmt.Println("worker panicked:", err)
}
}- The result channel is buffered, so the goroutine never blocks trying to
send even if you never call the
Responder. - A panic inside the
Workeris recovered and delivered on the same channel as anerror— check with a type assertion to tell it apart from a normal result.
MIT — see LICENSE.