Built-in function

Go recover()

Stops a panic when called directly by a deferred function running during panic unwinding in the same goroutine. It returns the panic value in that setting; outside it, recover returns nil. Execution resumes by returning from the function that deferred the recovering call, not at the panic statement. Example prints failed.

Formal Syntax & Structure

go syntaxgo
recover()

Return Value

any

Panic value when recovery is effective, otherwise nil (legacy panic(nil) behavior can differ).

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  defer func() { fmt.Println(recover()) }()  panic("failed")}
Go Reference