Junior — Senior
Determining the possibility of uncatchable panic
livecode
Task condition
It is necessary to determine whether there exists a scenario in this program where a panic occurs that is not recovered by recover().
package main
import (
"fmt"
"sync"
"time"
)
func main() {
wg := sync.WaitGroup{}
ch := make(chan struct{}, 5)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
printNumber(ch, i)
}()
}
wg.Wait()
close(ch)
}
func printNumber(ch chan struct{}, n int) {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
ch <- struct{}{}
time.Sleep(time.Second)
fmt.Println(1 / n)
<-ch
}