使用stringer生成错误码的String()方法,使得错误码实现fmt.Stringer的接口。code到错误描述的映射关系。
安装
go install golang.org/x/tools/cmd/stringer@latest
我们先自定义一个实现error接口的错误类型。如下所示。
package errx
import (
"fmt"
"strings"
)
type ErrCode int
func (e ErrCode) Error() string {
return fmt.Sprintf("%d@%s", e, e.String())
}
func ErrorIs(err error, code ErrCode) bool {
if err == nil {
return false
}
str := err.Error()
str = str[:strings.Index(str, "@")]
return str == fmt.Sprintf("%d", code)
}
我们现在先定义几个错误码,将错误描述通过注释的方式写在常量后面,如下所示。
package errx
//go:generate stringer -type ErrCode -linecomment -output code_string.go
const (
OK ErrCode = (iota + 1000) * -1 //请求OK
ErrParams //请求参数出错
ErrTimeout //请求超时
)
在当前目录下使用go generate生成
无输出表示成功的生成了,目录下多了个
code_string.go的文件
// Code generated by "stringer -type ErrCode -linecomment -output code_string.go"; DO NOT EDIT.
package errx
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[OK - -1000]
_ = x[ErrParams - -1001]
_ = x[ErrTimeout - -1002]
}
const _ErrCode_name = "请求超时请求参数出错请求OK"
var _ErrCode_index = [...]uint8{0, 12, 30, 38}
func (i ErrCode) String() string {
i -= -1002
if i < 0 || i >= ErrCode(len(_ErrCode_index)-1) {
return "ErrCode(" + strconv.FormatInt(int64(i+-1002), 10) + ")"
}
return _ErrCode_name[_ErrCode_index[i]:_ErrCode_index[i+1]]
}
可以看出来是通过索引去截取_ErrCode_name这个字串来使得code与描述对应。