Updated April 14, 2023
Introduction to Golang Panic
In Go language the panic is used to handle exceptions, It works similarly the try catch of other languages, the conditions like opening any file and the file is not available at the folder then we will get an error and the execution of the code will be halted and we do not want to halt the execution of the code, so we can use the panic to deal with these things, not only the handling we can also represent the error in a better format to make sense for the external users instead of displaying the error in the format as the occurs.
Syntax
Below is a simple syntax for the panic in the go language, here we can call according to correct conditions, so for example if we are going to read the name of the user and the name is nil, in that case, there may be possible that it will throw an error, so we can check the condition and call the panic function with the message which we want to show the external user. To use the panic we need to import it as along with the function at the initial time, and we can pass the parameters to a function and perform the checking of the functions and we can decide the condition on which we have to call the panic function. Like param==nil and call the panic function.
Please see the below syntax here the message can be any proper error message like “Null value not allowed” etc.
func panic(Message )
How does Panic Work in Go Language?
Before discussing the working of the panic we need to understand why we need the function panic? Suppose you are going to display the name of the users from the array of the data and the array contains the many users and in some cases any username is blank and we are performing some operation in that case nil will throw an error which will halt the execution of the whole program. So to deal with such types of situations we use the panic to handle just like any other programming language exceptions. We can explain the working of the panic in the following steps.
- We need to import the panic package within the function and we can pass some params also to the function.
- We can call the function and inside the function, we can write the conditions when the panic will get called and the message also we can write which we wanted to display in case of any error or exception.
- That we can pass any kind of argument, it can be any string of the message or it can be some other kind of numeric data.
- In very simple words I can tell you if anything which we want to handle dynamically then we can use the panic function of the go language.
- Remember the panic is a builtin function inside the language which we need to import to use.
Examples of Golang Panic
Below are some of very simple examples where we are showing examples without the panic and what changes will come after we use the panic to handle the situations like using the array more than the size of the array and passing nil as the params. In case if we want to execute the below examples we can make a file with any name like panic.go and we can run the command go run panic.go and we get the output for the executions.
Example #1
Below is a simple example where we are not handling any condition and we have defined the size of the array as the 4 and we are trying to print the 5th value which is throwing an error as we said here we are not handling error so we can see the unhandled error in that condition. We can see the below example along with the screen of the output for this case.
Code:
package main
import "fmt"
func main() {
var myname [4]string
myname[0] = "Sujoy"
myname[1] = "Vijay"
myname[2] = "Akash"
myname[3] = "Sujit"
fmt.Println("Attributes of the given array is:")
fmt.Println("First attribute is: ", myname[0])
fmt.Println("Second attribute is: ", myname[5])
}
Output:
Example #2
In the below example we are handling the nil value with the help of the panic function.
Code:
package main
import "fmt"
func build(the_type *string, name *string) {
if the_type == nil {
panic("Error: Type of education can not be nil")
}
if name == nil {
panic("Error: Language name cannot be blank")
}
fmt.Printf("Programing language name is: %s \n Language Name is: %s\n", *the_type, *name)
}
func main() {
the_type := "Programing"
build(&the_type, nil)
}
Output:
Example #3
Code:
package main
import "fmt"
func build(the_type *string, name *string) {
if the_type == nil {
panic("Error: Type of education can not be nil")
}
if name == nil {
panic("Error: Language name cannot be blank")
}
fmt.Printf("Programing language name is: %s \n Language Name is: %s\n", *the_type, *name)
}
func main() {
the_type := "Programing"
defer fmt.Println("We are using the defer function in main")
build(&the_type, nil)
}
Output:
Example #4
Below is an example where we are handling the nil condition with panic.
package main
import "fmt"
func call(user_type *string, name *string) {
if user_type == nil {
panic("Error: User type can not be nil")
}
if name == nil {
panic("Error: User name cannot be blank")
}
fmt.Printf("User Type is: %s \n User Name is: %s\n", *user_type, *name)
}
func main() {
user_type := "Programing"
defer fmt.Println("We are using the defer function in main")
call(&user_type, nil)
}
Output:
Recommended Articles
This is a guide to Golang Panic. Here we also discuss the introduction, syntax, and working of panic in the go language along with different examples and its code implementation. You may also have a look at the following articles to learn more –