Updated May 30, 2023
Definition of Golang errors
Gloang error is the most important package to handle errors and display them in a better way. We can pass any string message to the error using syntax as the errors.new(msg), here msg can be any message related to an error that occurs, it is very flexible, and with the help of this, we do not require to add any external error handling framework as it is an inbuilt package, we can use the error in the case when we wanted to avoid some uncertain conditions like division by 0 and calculation of square root of any negative value, and we can create a beautiful message for external user understanding.
Syntax of Golang errors
Below is a simple syntax for the go language; we can explain the below syntax in the following way,
- First, we need to import the errors package from go, and on New, we can pass the error message which we want to display to the users.
- Second, whatever error we will return to the calling function, the calling function can capture the return as the res, error =return error function.
- Once we print the error, then we will get the error message in which we have a pass at the time of errors.new(message) ,
- It allows us to deal with the errors which are not in proper ways, like performing the division with 0 will give us a run time error, and that will also stop the execution of the code.
Please see the below.
errors.New(message)
How does error() Work in Go Language?
Before discussing the working of the error in the go language, we need to understand why we need this; if we see in the many other programming languages, then we will find that there are many calculations like getting the division of two numbers like a/b and if b is 0 then it will be blunder so to avoid such type of the mistakes we use the concept of the error, error allows us to display the error and send it to calling function and calling function can capture the result. Let us discuss some of the important ways how it works.
- First, we need to import the package name errors.
- Important packages with name errors can be worked as errors.New(msg). Here msg is any message which we want to display in case any exceptions happen.
- We will keep this in a conditional area where we can call the errors.new() when it really needed.
- Once it captures the error and creates the message, it will return the message to the calling function, and the calling function can get the data in the second params.
- Second params like res ,err =function-calculating-or-returning. Here The function will return two things. One is the result, so if there was no error, in that case, it will simply return the result, or if the error occurs, the case error will have the message which sends by the method returns the error. We will learn more about it in the examples.
Examples of Golang error()
Below we have given some important examples where we are calculating and performing some arithmetic operations. In the examples, we are returning the output of error or result. In case we want to see the result of the output of the below examples, we can create a file called error. Go and copy and paste the examples on the file and can see the outputs.
Example #1
Code:
package main
import (
"fmt"
"math"
"errors"
)
func squareRoot(number float64)(float64, error) {
//checking if the number is negative
if(number < 0){
//We are returning from here as we found negative number
return 0, errors.New("This is a Negative value")
}
//returning for non negative number
res:= math.Sqrt(number)
return res,nil
}
func main() {
output, er:= squareRoot(-1)
if er != nil {
//print the error details
fmt.Println("The error while calculating square root is",er)
} else {
//print success
fmt.Println("The result of the square root calculation is",output)
}
output, er = squareRoot(9)
if er != nil {
//print the error details
fmt.Println("The error while calculating square root is",er)
} else {
//print success
fmt.Println("The result of the square root calculation is",output)
}
}
Output:
Example #2
Code:
package main
import "errors"
import "fmt"
func subtraction(a,b float64)(float64, error) {
if(a < b){
return 0, errors.New("Math: The value a is greater then b")
}
return a-b, nil
}
func main() {
output, er:= subtraction(4,5)
if er != nil {
fmt.Println("The error is",er)
} else {
fmt.Println("The result of the subtraction is",output)
}
output, er = subtraction(90,11)
if er != nil {
fmt.Println("The error is",er)
} else {
fmt.Println("The result of the subtraction is",output)
}
}
Output:
Example #3
Code:
package main
import "errors"
import "fmt"
func divide(a,b float64)(float64, error) {
if(b==0){
return 0, errors.New("Math: value of b is 0,division not allowed")
}
return a/b, nil
}
func main() {
output, er:= divide(4,0)
if er != nil {
fmt.Println("The error is",er)
} else {
fmt.Println("The result of the division is",output)
}
output, er = divide(90,10)
if er != nil {
fmt.Println("The error is",er)
} else {
fmt.Println("The result of the division is",output)
}
}
Output:
Example #4
Code:
package main
import "errors"
import "fmt"
func percentage(a,r float64)(float64, error) {
if(r==0){
return 0, errors.New("Math: rate can not be 0")
}
return (a*r)/100, nil
}
func main() {
output, er:= percentage(4,0)
if er != nil {
fmt.Println("The error is",er)
} else {
fmt.Println("The result of the percentage is",output)
}
output, er = percentage(90,10)
if er != nil {
fmt.Println("The error is",er)
} else {
fmt.Println("The result of the percentage is",output)
}
}
Output:
Recommended Articles
This is a guide to Golang error. Here we also discuss the definition, syntax, and working of error() in go language along with different examples and its code implementation. You may also have a look at the following articles to learn more –