How to create errors in golang

Release: 2019-12-07 13:21:40
Original
5635 people have browsed it

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
}
Copy after login

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)
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template