Go

Go

By DeepSource

Bad use of recover() GO-W1030

Bug risk

According to the go documentation of recover, executing a call to recover inside a deferred function (but not any function called by it) stops the panicking sequence by restoring normal execution and retrieves the error value passed to the call of panic. If recover is called outside the deferred function, it will not stop a panicking sequence.

Calling recover immediately after a go or a defer statement will have no effect.

Bad practice

package main

func foo() {
    defer recover()
    go recover()
}

Recommended

package main

func foo() {
    defer func() {
        if r := recover(); r != nil {
            // panic recovered, do something
        }
    }()
}