Our website uses cookies to enhance your browsing experience.
Accept
to the top

Webinar: Let's make a programming language. Lexer - 29.04

>
>
>
V8007. Calling the 'recover'...
menu mobile close menu
Additional information
toggle menu Contents

V8007. Calling the 'recover' function inside the anonymous function that is not deferred will not recover execution from panic.

Apr 03 2026

The analyzer has detected the use of the recover function within an anonymous function that is called immediately. So, if a panic occurs, the goroutine cannot resume execution.

Look at the synthetic example:

func foo() {
  func() {
    if r := recover(); r != nil {
      fmt.Println("Recovered", r)
    }
  }()
  q, r := bits.Div64(hi, lo, y)
  ....
}

After the code defines the anonymous function and calls it, the bits.Div64 function is called. This function can trigger a panic if y equals 0. If that panic reaches the top of the stack, the program will crash.

Call recover after the panic occurs to restore execution. In the current code, the recover function runs before any possible panic because it is defined within an immediately-called anonymous function. As a result, nothing gets recovered.

You can fix this by deferring the anonymous function via the defer keyword:

func foo() {
  defer func() {
    if r := recover(); r != nil {
      fmt.Println("Recovered", r)
    }
  }()
  q, r := bits.Div64(hi, lo, y)
}