登录
首页 >  Golang >  Go问答

Go语言实现多级错误类型的方法

来源:stackoverflow

时间:2024-03-09 23:06:27 253浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《Go语言实现多级错误类型的方法》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我试图在 go 中创建错误的子类型。我之前就此事问过一个问题。

现在我面临着多种类型的问题。以下代码显示了错误类型定义:

/* interfaces */
type universalerror interface {
    commonerror1
}

type commonerror1 interface {
    error
    commonerror1()
}

/* structs */
type error1 struct {
    reason string
}

type error2 struct {
    reason string
}

type error3 struct {
    reason string
}

/* interface function implementations */
func (error1 error1) error() string {
    return fmt.sprintf(error1.reason)
}

func (error2 error2) error() string {
    return fmt.sprintf(error2.reason)
}

func (error3 error3) error() string {
    return fmt.sprintf(error3.reason)
}

func (error1) commonerror1() {}
func (error2) commonerror1() {}
func (error3) universalerror() {}

当我尝试运行以下代码时:

func main() {
    var err1 = error1{reason: "error reason 1"}
    var err2 = error2{reason: "error reason 2"}
    var err3 = error3{reason: "error reason 3"}

    fmt.println("\n**** types *****")
    printtype(err1)
    printtype(err2)
    printtype(err3)
}

func printtype(param error) {
    switch param.(type) {
    case universalerror:
        switch param.(type) {
        case commonerror1:
            switch param.(type) {
            case error1:
                fmt.println("error1 found")
            case error2:
                fmt.println("error2 found")
            default:
                fmt.println("commonerror1 found, but does not belong to error1 or error2")
            }
        default:
            fmt.println("error3 found")
        }
    default:
        fmt.println("error belongs to an unidentified type")
    }
}

printtype() 函数打印以下内容:

**** Types *****
Error1 found
Error2 found
CommonError1 found, but Does not belong to Error1 or Error2

我需要将 error3 的类型标识为 universalerror,而不是 commonerror1。我怎样才能做到这一点?我的方法有什么问题吗?


解决方案


您使用了 universalerror() 方法,但您没有将其添加到接口“定义”中,因此请执行以下操作:

type universalerror interface {
    commonerror1
    universalerror()
}

并且您希望 error3 成为 universalerror。要使 error3 成为 universalerror,它必须实现其所有方法:universalerror()commonerror1()。所以你必须添加这两个方法:

func (error3) commonerror1()   {}
func (error3) universalerror() {}

进行这些更改后,输出将是(在 Go Playground 上尝试):

**** types *****
error belongs to an unidentified type
error belongs to an unidentified type
commonerror1 found, but does not belong to error1 or error2

提示:如果您希望编译时保证某些具体类型实现某些接口,请使用如下所示的空白变量声明:

var _ universalerror = error3{}

上面的声明将 error3 的值赋给 universalerror 类型的变量。如果 error3 不满足 universalerror,则会出现编译时错误。上面的声明不会引入新变量,因为使用了 blank identifier,这只是一个编译时检查。

如果您要删除 error3.commonerror1() 方法:

//func (error3) commonerror1()   {}
func (error3) universalerror() {}

然后你会立即得到一个编译时错误:

./prog.go:49:5: cannot use Error3 literal (type Error3) as type UniversalError in assignment:
    Error3 does not implement UniversalError (missing CommonError1 method)

理论要掌握,实操不能落!以上关于《Go语言实现多级错误类型的方法》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>