Inspectopedia Help

Defer/go statement calls 'recover' or 'panic' directly

Reports defer and go statements that call panic() or recover() directly.

Such statements are rarely useful and might indicate a misuse of the panic() and recover() mechanism. In particular:

  • go panic(): a newly-started goroutine will panic immediately.

  • defer panic(): a function with this statement will always panic on exit.

  • go recover(): has no effect as newly-started goroutine cannot panic.

  • defer recover(): function with this statement will silently stop panicking. This could be a valid usage, but an idiomatic way is to inspect the value returned by recover():

    defer func() { if r := recover(); r != nil { fmt.Println("Recovered from: ", r) } }()

For more information about go statements and panics handling, see Handling panics and Go statements in the Go Language Specification.

Inspection Details

Available in:

GoLand 2023.3

Plugin:

Go, 233.SNAPSHOT

Last modified: 13 July 2023