How to Create Custom Errors using New Function in Golang? Last Updated : 15 Apr, 2020 Comments Improve Suggest changes 1 Likes 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! Create Quiz Comment S supergops Follow 1 Improve S supergops Follow 1 Improve Article Tags : Go Language Write From Home Explore OverviewGo Programming Language (Introduction) 7 min read How to Install Go on Windows? 3 min read How to Install Golang on MacOS? 4 min read Hello World in Golang 3 min read FundamentalsIdentifiers in Go Language 3 min read Go Keywords 2 min read Data Types in Go 7 min read Go Variables 9 min read Constants- Go Language 6 min read Go Operators 9 min read Control StatementsGo Decision Making (if, if-else, Nested-if, if-else-if) 5 min read Loops in Go Language 5 min read Switch Statement in Go 2 min read Functions & MethodsFunctions in Go Language 3 min read Variadic Functions in Go 3 min read Anonymous function in Go Language 2 min read main and init function in Golang 2 min read What is Blank Identifier(underscore) in Golang? 3 min read Defer Keyword in Golang 3 min read Methods in Golang 3 min read StructureStructures in Golang 7 min read Nested Structure in Golang 3 min read Anonymous Structure and Field in Golang 3 min read ArraysArrays in Go 7 min read How to Copy an Array into Another Array in Golang? 3 min read How to pass an Array to a Function in Golang? 2 min read SlicesSlices in Golang 14 min read Slice Composite Literal in Go 3 min read How to sort a slice of ints in Golang? 2 min read How to trim a slice of bytes in Golang? 3 min read How to split a slice of bytes in Golang? 3 min read StringsStrings in Golang 7 min read How to Trim a String in Golang? 2 min read How to Split a String in Golang? 3 min read Different ways to compare Strings in Golang 2 min read PointersPointers in Golang 8 min read Passing Pointers to a Function in Go 3 min read Pointer to a Struct in Golang 3 min read Go Pointer to Pointer (Double Pointer) 4 min read Comparing Pointers in Golang 3 min read ConcurrencyGoroutines - Concurrency in Golang 2 min read Select Statement in Go Language 4 min read Multiple Goroutines 2 min read Channel in Golang 7 min read Unidirectional Channel in Golang 2 min read Like