Home  >  Article  >  Backend Development  >  How to create errors in golang

How to create errors in golang

尚
Original
2019-12-07 13:21:405606browse

How to create errors in golang

In this tutorial, we will learn how to create our own custom errors and use it in the functions and packages we create. We'll use the same techniques as in the standard library to provide more detailed information on custom errors.

Use the New function to create custom errors

The easiest way to create a custom error is to use the New function in the errors package.

Before using the New function to create a custom error, let’s first take a look at how New is implemented. As shown below, it is the implementation of the New function in the errors package.

// Package errors implements functions to manipulate errors.
package errors
// New returns an error that formats as the given text.
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
}

The implementation of the New function is very simple. errorString is a structure type with only one string field s. Line 14 uses the errorString pointer receiver (Pointer Receiver) to implement the Error() string method of the error interface.

The New function in line 5 has a string parameter, through which a variable of type errorString is created and its address is returned. So it creates and returns a new error.

Now that we know how the New function works, let’s start using New in the program to create custom errors.

We will create a simple program that calculates the radius of a circle and returns an error if the radius is negative.

package main
import (  
    "errors"
    "fmt"
    "math"
)
func circleArea(radius float64) (float64, error) {  
    if radius < 0 {
        return 0, errors.New("Area calculation failed, radius is less than zero")
    }
    return math.Pi * radius * radius, nil
}
func main() {  
    radius := -20.0
    area, err := circleArea(radius)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("Area of circle %0.2f", area)
}

Running on glayground

In the above program, we check if the radius is less than zero (line 10). If the radius is less than zero, we return an area equal to 0, along with an appropriate error message. If the radius is greater than zero, the area is calculated and a nil error is returned (line 13).

In the main function, we check if the error is equal to nil on line 19. If it is not nil, we print an error and return it, otherwise we print the area of ​​the circle.

In our program, the radius is less than zero, so it prints out:

Area calculation failed, radius is less than zero

## Recommended:

go language tutorial

The above is the detailed content of How to create errors in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn