How to Create Custom Errors using New Function in Golang? Last Updated : 15 Apr, 2020 Comments Improve Suggest changes Like Article Like Report Custom errors are those that can be defined by the user. For example, if one writes a function that is used to divide two numbers, which is to be used in multiple places, the user could create a custom error that can be set whenever division by zero is encountered, for it could prove to be fatal in the long run. The easiest way to create custom errors in Golang is to use the New() function which is present in the errors module of Golang. Before actually moving on to creating our own error, let's have a look at how it is implemented in the errors package. C package errors // New returns an errorString // that contains the input // text as its value. func New(text string) error { return &errorString{text} } // errorString is a trivial // implementation of error. type errorString struct { s string } func (e *errorString) Error() string { return e.s } So, basically what the New() function does is create an error with a custom message that can be written by the programmer. For example, let's say we want an error to be displayed whenever a negative value is given as input for the program that outputs the area of a square, given its side. C package main // the errors package // contains the New function import ( "errors" "fmt" ) func main() { side := -2.0 if side < 0.0 { // control enters here if error condition is met fmt.Println(errors.New("Negative value entered!")) return } fmt.Printf("Area= %f", side*side) } Output: Negative value entered! This was so because we had entered the length of side as -2, which cannot be possible, and hence the error was thrown. Now let's try out a similar program but this time let's put the error generation in a separate function. CPP package main import ( "errors" "fmt" ) func findAreaSquare(side float64) (error, float64) { if side < 0.0 { // function that returns the error // as well as the computed area return errors.New("Negative value entered!"), 0 } return nil, side * side } func main() { side := -2.0 err, area := findAreaSquare(side) if err != nil { fmt.Printf("%s", err) return } fmt.Printf("Area= %f", area) } Output: Negative value entered! Comment More infoAdvertise with us Next Article How to Create Custom Errors using New Function in Golang? S supergops Follow Improve Article Tags : Go Language Write From Home Similar Reads How to use strconv.FormatBool() Function in Golang? Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a FormatBool() function which is used to return true or false according to the value of x. To access FormatBool() function you need to import 2 min read reflect.New() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package.The reflect.New() Function in Golang is used to get the Value representing a pointer to a new zero value for the specified typ 2 min read How To Create Custom Error Handler Middleware in Express? In ExpressJS there is a built-in class named CustomError which is basically used to create an error of your choice. in this article, we will see how with the use of this CustomError class we can create a custom error handler middleware in ExpressJS.What is a CustomError Handler?A custom error handle 5 min read errors.New() Function in Golang with Examples Errors package in Golang is used to implement the functions to manipulate the errors. errors.New()function returns an error that formats like given text. Each call to New returns distinct error value indeed in the event that the content is indistinguishable. Syntax: func New(text string) error It re 1 min read reflect.NewAt() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.NewAt() Function in Golang is used to get the Value representing a pointer to a value of the specified type, usin 2 min read Like