Type of error in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Type of error in Golang

The error type itself is a predefined interface that defines a method

type error interface {    Error() string}

Generates a new error and returns

In general, there are several ways to handle this:

package mainimport ("errors""fmt")type Customerror struct {infoa stringinfob stringErr   error}func (cerr Customerror) Error() string {errorinfo := fmt.Sprintf("infoa : %s , infob : %s , original err info : %s ", cerr.infoa, cerr.infob, cerr.Err.Error())return errorinfo}func main() {//方法一://采用errors包的New方法 返回一个err的类型var err error = errors.New("this is a new error")//由于已经实现了error接口的方法 因此可以直接调用对应的方法fmt.Println(err.Error())//方法二://采用fmt.Errof 将string信息转化为error信息 并返回err = fmt.Errorf("%s", "the error test for fmt.Errorf")fmt.Println(err.Error())//方法三://采用自定义的方式实现一个error的 一个duck 类型err = &Customerror{infoa: "err info a",infob: "err info b",Err:   errors.New("test custom err"),}fmt.Println(err.Error())}/*output:this is a new errorthe error test for fmt.Errorfinfoa : err info a , infob : err info b , original err info : test custom err */

The error package content in Golang is also relatively simple, and this package implements the method (error) declared in error equivalent to the duck type of an error interface.

// 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}

Using FMT. The Errorf method converts the string type to the error type, and within this method, calls the sprintf method in the FMT package to convert the formatted input to a string, using errors. The New method returns the error type.

A custom error type can be used to determine the dynamic type of err before the next layer of judgment.
such as net. The error interface is a re-encapsulation of the original error interface. Determines the IO at the end of the reader reading when reading the file. Eof.

//io.EOFvar EOF = errors.New("EOF")//net.Error type Error interface {errorTimeout() bool   // Is the error a timeout?Temporary() bool // Is the error temporary?}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.