如何在Go中创建多级错误子类型

如何在Go中创建多级错误子类型

本文介绍了如何在Go中创建多级错误子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在GO中创建错误的子类型. 我之前已经问过一个问题.

I was trying to create sub-types of errors in GO. I've asked a question previously regarding the matter.

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

Now I'am facing an issue with the multiple types. Following code shows the error type definitions:

/* 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() {}

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

When I try to run the following code:

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.我怎样才能做到这一点?我的方法有什么问题吗?

I need the type of Error3 to be identified as an UniversalError, but not as a CommonError1. How can I achieve this ? Is there anything wrong in my approach ?

推荐答案

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

You use the UniversalError() method, but you did not add that to the interface "definition", so do that:

type UniversalError interface {
    CommonError1
    UniversalError()
}

并且您希望Error3成为UniversalError.为了使Error3成为UniversalError,它必须实现其所有方法:UniversalError()CommonError1().因此,您必须同时添加这两种方法:

And you want Error3 to be a UniversalError. For Error3 to be a UniversalError, it must implement all its methods: UniversalError() and CommonError1(). So you have to add both those methods:

func (Error3) CommonError1()   {}
func (Error3) UniversalError() {}

进行这些更改后,输出将是(在游乐场上尝试):

With these changes, output will be (try it on the 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

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

Tip: If you want compile-time guarantee that some concrete type implements some interface, use a blank variable declaration like this:

var _ UniversalError = Error3{}

上面的声明将Error3的值分配给UniversalError类型的变量.如果Error3不满足UniversalError,则会出现编译时错误.上面的声明不会引入新变量,因为使用了空白标识符,这只是一个编译时检查.

The above declaration assigns a value of Error3 to a variable of type UniversalError. Should Error3 not satisfy UniversalError, you get a compile-time error. The above declaration will not introduce a new variable as the blank identifier was used, this is just a compile-time check.

如果要删除Error3.CommonError1()方法:

//func (Error3) CommonError1()   {}
func (Error3) UniversalError() {}

然后您将立即收到编译时错误:

Then you would get a compile-time error right away:

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

这篇关于如何在Go中创建多级错误子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:44