3 ms·
Defer and recover are guaranteed by the runtime, so deferring a recovery that launches a goroutine which sends a value on a child-died channel is not quite a bo
by JulianMorrison 14y ago
Defer and recover are guaranteed by the runtime, so deferring a recovery that launches a goroutine which sends a value on a child-died channel is not quite a bodge. However as you point out unlike Erlang there are no PIDs, so you'd either need a channel per child or an ID per child. And the whole thing would be very, very manual.
Something like
type Crash struct {
PID int
Panic interface{}
}
var crashed = make(chan Crash)
func parent() {
go child()
oops := <-crashed
panic(oops)
}
func child() {
defer func() {
if r := recover(); r != nil {
crashed <- Crash{1, r}
}
}
panic("boom")
}