Golang 中具体类型的错误片段
我正在 go 中尝试错误包装,并有一个返回包装的自定义错误类型的函数。我想做的是迭代预期错误列表并测试函数的输出是否包含这些预期错误。
我发现将自定义错误放入 []error
意味着自定义错误的类型将为 *fmt.wraperror
,这意味着 errors.as()
几乎总是返回 true。
作为示例,请考虑以下代码:
package main import ( "errors" "fmt" ) type anothererror struct { } func (e *anothererror) error() string { return "another error" } type missingattrerror struct { missingattr string } func (e *missingattrerror) error() string { return fmt.sprintf("missing attribute: %s", e.missingattr) } func dosomething() error { e := &missingattrerror{missingattr: "key"} return fmt.errorf("dosomething(): %w", e) } func main() { err := dosomething() expectederrone := &missingattrerror{} expectederrtwo := &anothererror{} expectederrs := []error{expectederrone, expectederrtwo} fmt.printf("is err '%v' type '%t'?: %t\n", err, expectederrone, errors.as(err, &expectederrone)) fmt.printf("is err '%v' type '%t'?: %t\n", err, expectederrtwo, errors.as(err, &expectederrtwo)) for i := range expectederrs { fmt.printf("is err '%v' type '%t'?: %t\n", err, expectederrs[i], errors.as(err, &expectederrs[i])) } }
其输出为
is err 'dosomething(): missing attribute: key' type '*main.missingattrerror'?: true is err 'dosomething(): missing attribute: key' type '*main.anothererror'?: false is err 'dosomething(): missing attribute: key' type '*fmt.wraperror'?: true is err 'dosomething(): missing attribute: key' type '*fmt.wraperror'?: true
理想情况下我希望输出是
Is err 'DoSomething(): missing attribute: Key' type '*main.MissingAttrError'?: true Is err 'DoSomething(): missing attribute: Key' type '*main.AnotherError'?: false Is err 'DoSomething(): missing attribute: Key' type '*main.MissingAttrError'?: true Is err 'DoSomething(): missing attribute: Key' type '*main.AnotherError'?: false
出现错误的原因是我希望能够为每个测试用例条目定义预期错误的列表。假设我知道为函数提供某些输入将使其沿着一条路径返回包含特定错误的错误。
如何将 *fmt.wraperror
类型从 []error
切片转换回原始类型,以便我可以将其与 error.as
一起使用?
我知道我可以使用 将其强制转换为特定类型。(anothererror)
,但为了在迭代切片时使其工作,我必须对函数可能返回的每个可能的错误执行此操作,不是吗?) 将其强制转换为特定类型。(anothererror)
,但为了在迭代切片时使其工作,我必须对函数可能返回的每个可能的错误执行此操作,不是吗?)
正确答案
您可以使用以下方法欺骗 errors.as
正确答案
errors.as
您可以使用以下方法欺骗
func main() { err := DoSomething() m := &MissingAttrError{} a := &AnotherError{} expected := []any{&m, &a} for i := range expected { fmt.Printf("Is err '%v' type '%T'?: %t\n", err, expected[i], errors.As(err, expected[i])) } }
errors.as
的是 *error
。因此,包装的错误值(即 err
)被直接分配给目标值。在我的示例中,传递给 errors.as
的值是 **anothererror
,并且 err
不能分配给 *anothererror
打印的类型不是您所期望的,但 按其应有的方式工作。🎜
🎜您的示例不起作用的原因是您传递给 🎜 的是 *error
。因此,包装的错误值(即 err
)被直接分配给目标值。在我的示例中,传递给 🎜 的值是 **anothererror
,并且 err
不能分配给 *anothererror
。🎜以上是Golang 中具体类型的错误片段的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

OpenSSL,作为广泛应用于安全通信的开源库,提供了加密算法、密钥和证书管理等功能。然而,其历史版本中存在一些已知安全漏洞,其中一些危害极大。本文将重点介绍Debian系统中OpenSSL的常见漏洞及应对措施。DebianOpenSSL已知漏洞:OpenSSL曾出现过多个严重漏洞,例如:心脏出血漏洞(CVE-2014-0160):该漏洞影响OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻击者可利用此漏洞未经授权读取服务器上的敏感信息,包括加密密钥等。

在BeegoORM框架下,如何指定模型关联的数据库?许多Beego项目需要同时操作多个数据库。当使用Beego...

后端学习路径:从前端转型到后端的探索之旅作为一名从前端开发转型的后端初学者,你已经有了nodejs的基础,...

Go语言中使用RedisStream实现消息队列时类型转换问题在使用Go语言与Redis...

GoLand中自定义结构体标签不显示怎么办?在使用GoLand进行Go语言开发时,很多开发者会遇到自定义结构体标签在�...

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

本文介绍如何在Debian系统上配置MongoDB实现自动扩容,主要步骤包括MongoDB副本集的设置和磁盘空间监控。一、MongoDB安装首先,确保已在Debian系统上安装MongoDB。使用以下命令安装:sudoaptupdatesudoaptinstall-ymongodb-org二、配置MongoDB副本集MongoDB副本集确保高可用性和数据冗余,是实现自动扩容的基础。启动MongoDB服务:sudosystemctlstartmongodsudosys
