The error data type

Many occasions exist where you might end up having to deal with a new error case while developing your own Go application. The error data type is here to help you define your own errors.

This subsection will teach you how to create your own error variables. As you will see in a while, in order to create a new error variable, you will need to call the New() function of the errors standard Go package.

The example Go code illustrating this process can be found in newError.go, and it will be presented in two parts. The first part of the program follows next:

package main 
 
import ( 
    "errors" 
    "fmt" 
) 
 
func returnError(a, b int) error { 
    if a == b { 
        err := errors.New("Error in returnError() function!") 
        return err 
    } else { 
        return nil 
    } 
} 

There are many interesting things happening here. First of all, you can see the definition of a Go function other than main() for the first time in this book. The name of this new unsophisticated function is returnError(). Additionally, you can see the errors.New() function in action, which takes a string value as its parameter. Last, if a function should return an error variable but there is no error to report, it returns nil instead.

You will learn more about the various types of Go functions in Chapter 6, What You Might Not Know About Go Packages.

The second part of newError.go is as follows:

func main() { 
    err := returnError(1, 2) 
    if err == nil { 
        fmt.Println("returnError() ended normally!") 
    } else { 
        fmt.Println(err) 
    } 
 
    err = returnError(10, 10) 
    if err == nil { 
        fmt.Println("returnError() ended normally!") 
    } else { 
        fmt.Println(err) 
    } 
 
    if err.Error() == "Error in returnError() function!" { 
        fmt.Println("!!") 
    } 
} 

As the code illustrates, most of the time, you need to check whether an error variable is equal to nil or not and then act accordingly. Also presented here is the use of the err.Error() method, which allows you to convert an error variable into a string variable. This function lets you compare an error variable with a string.

Sending your error messages to the logging service of your Unix machine, especially when a Go program is a server or some other critical application. However, the code presented in this book will not follow this principle everywhere in order to avoid filling your log files with unnecessary data.

Executing newError.go will produce the following output:

$ go run newError.go
returnError() ended normally!
Error in returnError() function!
!!  

If you try to compare an error variable with a string variable without converting the error variable to a string first, the Go compiler will create the following error message:

# command-line-arguments
./newError.go:33:9: invalid operation: err == "Error in returnError() 
function!" (mismatched types error and string)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.57