Panic
suggest changeA panic halts normal execution flow and exits the current function.
Any deferred calls will then be executed before control is passed to the next function higher on the stack.
Each function on the stack exits and runs its deferred calls until the panic is handled by a deferred recover, or until the panic finishes main and terminates the program.
If this occurs, the argument provided to panic and a stack trace will be printed to stderr.
func foo() {
defer fmt.Println("Exiting foo")
panic("bar")
}
func main() {
defer fmt.Println("Exiting main")
foo()
}
Exiting foo
Exiting main
panic: bar
goroutine 1 [running]:
main.foo()
/tmp/src407264016/main.go:10 +0xb9
main.main()
/tmp/src407264016/main.go:15 +0xa2
exit status 2
panic accepts any type as its parameter.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents