Go has a Panic(msg)
function. If the panic function is called, execution of the program is stopped. The panic function has a parameter: a message to show.
You can use the panic function if a failure occurs that you don’t want or don’t know how to deal with.
Example
Introduction
In the most basic scenario, you call the panic function.
Call the panic function is a simple as this code:
package main |
In a terminal, it shows:
1 | $ go run demo.go |
Panic function
Real life scenario: Your program needs to create a file, but you don’t want to deal with error processing.
The panic()
function will make the program exit if it cannot create the file.
package main |
1 | $ go run demo.go |
Leave a Reply