当前位置: 首页 > 面试题库 >

我们可以为Go中的错误创建子类型吗?

丌官昊天
2023-03-14
问题内容

我想在Go中创建分层错误。我们可以在Go中实现吗?例如,我有以下两个错误。

type Error1 struct {
    reason string
    cause error
}

func (error1 Error1) Error() string {
    if error1.cause == nil || error1.cause.Error() == "" {
        return fmt.Sprintf("[ERROR]: %s\n", error1.reason)
    } else {
        return fmt.Sprintf("[ERROR]: %s\nCaused By: %s\n", error1.reason, error1.cause)
    }
}

type Error2 struct {
    reason string
    cause error
}

func (error2 Error2) Error() string {
    if error2.cause == nil || error2.cause.Error() == "" {
        return fmt.Sprintf("[ERROR]: %s\n", error2.reason)
    } else {
        return fmt.Sprintf("[ERROR]: %s\nCause: %s", error2.reason, error2.cause)
    }
}

我想要一个CommonError包含两个子类型Error1和的错误类型Error1,以便执行以下操作。

func printType(param error) {
    switch t := param.(type) {
    case CommonError:
        fmt.Println("Error1 or Error 2 found")
    default:
        fmt.Println(t, " belongs to an unidentified type")
    }
}

有办法实现吗?

编辑:

在类型切换中,我们可以使用多个错误,如下所示: case Error1, Error2:但是,当我遇到大量错误时,或者当我需要对模块内部的错误进行抽象时,这种方法将不是最好的方法。


问题答案:

您可以在中列出多个类型case,这样便可以完成您想做的事情:

switch t := param.(type) {
case Error1, Error2:
    fmt.Println("Error1 or Error 2 found")
default:
    fmt.Println(t, " belongs to an unidentified type")
}

测试它:

printType(Error1{})
printType(Error2{})
printType(errors.New("other"))

输出(在Go Playground上尝试):

Error1 or Error 2 found
Error1 or Error 2 found
other  belongs to an unidentified type

如果要对错误进行“分组”,另一种解决方案是创建“标记”接口:

type CommonError interface {
    CommonError()
}

Error1Error2必须实现:

func (Error1) CommonError() {}

func (Error2) CommonError() {}

然后您可以执行以下操作:

switch t := param.(type) {
case CommonError:
    fmt.Println("Error1 or Error 2 found")
default:
    fmt.Println(t, " belongs to an unidentified type")
}

用相同的方法测试,输出是相同的。在Go Playground上尝试一下。

如果您想将CommonErrors 限制为“ true”错误,请同时嵌入error接口:

type CommonError interface {
    error
    CommonError()
}


 类似资料:
  • 问题内容: 我试图在GO中创建错误的子类型,现在,我面临多种类型的问题。以下代码显示了错误类型定义: 当我尝试运行以下代码时: 该函数打印以下内容: 我需要将的类型标识为,而不是。我怎样才能做到这一点?我的方法有什么问题吗? 问题答案: 您使用了该方法,但是没有将其添加到“定义”接口中,因此请执行以下操作: 而你想成为一个。对于成为一个,它必须实现所有的方法:和。因此,您必须同时添加这两种方法:

  • 萨拉姆! 在java中,我需要在该类中创建一个类的ArrayList。有可能吗???正如我所知,如果我在该类构造函数中创建一个类的对象,那么它将导致StackOverFlow。下面给出了问题的完整细节。如何在BookInfo类本身中创建该类的ArrayList??? 问题声明: 您需要编写一个java程序,该程序只包含两个名为BookInfo和BookMgtSys的类。 BookInfo类必须具有

  • 问题内容: 我知道我们可以重载类实例的行为,例如- 我们可以更改的结果print s: 我们可以更改结果print Sample吗? 问题答案: 您可以使用元类: Python 3: Python 2: 输出: 元类是类的类。它与类的关系类似于类与实例的关系。使用相同的class语句。type而是从继承表单object使其成为一个元类。按惯例self由代替cls。

  • 有什么方法可以为我创建的类使用自动装箱吗?例如,我有的子类。 现在,< code > UnsignedInteger I = new UnsignedInteger(88);工作得非常好,但是有什么方法可以让这个编译:< code > UnsignedInteger i = 88?对我来说不会。提前感谢!

  • 假设我有一个抽象类“车辆”和另外两个继承自名为“汽车”和“自行车”的车辆类的子类。 所以我可以随机创建一系列汽车和自行车对象: 并将它们全部添加到Arraylist: 是否有可能将这些对象写入单个文本文件,并根据特定的对象类型(汽车、自行车)从文本文件读取回Arraylist,而无需为每种类型维护不同的文本文件

  • 我必须从Postgres表中读取配置并广播它,以使用它过滤主数据流。我正在使用Flink广播状态进行此操作。当我从本地套接字获取配置时,它工作得很好。 用例是在Flink作业中从Postgres读取最新配置,而无需重新启动作业。 我们可以从Postgres表创建Flink数据流吗?如果可能的话,它是否有效,因为它将永远保持JDBC连接的活性?