Golang error解析

error是什么?

// The error built-in interface type is the conventional interface for
// representing an error condition, with the nil value representing no error.
type error interface {
	Error() string
}

可以看到,error就是一个接口,其中包含Error() string方法。从Error()的返回值也可以推断出,出现错误也只是打印错误字符串而已。

从接口类型可以很自然的想到,我们可以实现自己结构的Error() string方法,从而实现error接口。

创建error

errors.New()函数

我们常使用errors.New,这返回一个经过字符串填充过的errorString结构,我们可以打印错误信息。

package main

import (
	"errors"
	"fmt"
)

func main() {
	error := errors.New("this is a test")
	if error != nil {
		fmt.Println(error)         // this is a test
		fmt.Println(error.Error()) // this is a test
	}
}

errors的内部实现

// New returns an error that formats as the given text.
// Each call to New returns a distinct error value even if the text is identical.
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
}

可以看到,New接收一个字符串,填充并返回一个errorString结构,而errorString结构很简单,仅包含一个string类型。

还可以看到,errorString结构实现了自己的Error方法,返回自己接收到的错误信息。

自定义结构实现error

可以看到之前的errorString结构实现了自己的Error方法,从而实现了error接口。我们也可以自定义结构,实现自己的Error() string方法从而实现error接口。

package main

import "fmt"

type MyError struct {
	name string
	id   int
}

// MyError 实现 Error 方法 ---> 实现 error 接口
func (e MyError) Error() string {
	return fmt.Sprintf("%v: %v", e.name, e.id)
}

// MyError 实现了 error 接口,所以可以被转换为 error 类型
func getMyError() error {
	return MyError{
		name: "this is a test err",
		id:   1,
	}
}

func main() {
	if err := getMyError(); err != nil {
		fmt.Println(err) 
	}
	// this is a test err: 1
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值