If I do not understand your question, deferred function calls will be executed during a panic, even if the passed value was nil . This is illustrated by the following program :
package main import "fmt" func main() { defer func() { fmt.Println("Recover:", recover()) }() panic(nil) }
This way you can easily detect if panic(nil) happened by comparing the value returned with recover() to nil .
Change response to comment:
Yes it's true; deferred calls are usually made when the function returns. But they also start when unwinding the call stack after panic() .
Edit after question has been updated:
You are right that there is no way to differentiate these cases. On the other hand, panic with nil doesn't make much sense either - especially because of this limitation.
The only use case for panic(nil) that I could think of would be to intentionally prevent recovery and force a program crash using stack trace. There are more elegant ways to do this, though, for example, using the runtime package.
source share