I am new to go and learning defer. I am wondering why the first one works while the second one not. I wonder what's the difference between them. First:
func say(s string) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in cleanup:", r)
}
wg.Done()
}()
for i := 0; i < 3; i++ {
fmt.Println(s)
time.Sleep(time.Millisecond * 100)
if i == 2 {
panic("oh")
}
}
}
Second:
func cleanup(){
if r := recover(); r != nil {
fmt.Println("Recovered in cleanup:", r)
}
}
func say(s string) {
defer func() {
cleanup()
wg.Done()
}()
for i := 0; i < 3; i++ {
fmt.Println(s)
time.Sleep(time.Millisecond * 100)
if i == 2 {
panic("oh")
}
}
}