In this program, we will demonstrate the use of the panic() function. The use of the panic() function is to abort if a function returns an error value that we don't know how to handle? Here, we will call the abort function with the specified message in case of "divide by zero".
Program/Source Code:
The source code to demonstrate the panic() function is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package to formatting related functions.
In the main() function, we created three integer variables and read the value of num1, num2 from the user. Then we checked the value of variable num2. If it is 0 then we called the panic() function to abort the program and print the appropriate message on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer