gpt4 book ai didi

go - 这种类型断言的目的是什么

转载 作者:行者123 更新时间:2023-12-01 22:39:18 25 4
gpt4 key购买 nike

wrap file在错误包中,我看到了这种类型的断言:

if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
return true
}
这种形式的类型断言是什么,它与类似的东西有什么不同
type IsError interface{
Is(error) bool
}
if x, ok := err.(IsError); ok && x.Is(target) {
return true
}
它看起来几乎就像它试图断言 err是具有单一方法的接口(interface)文字 Is(error) bool ,但我认为接口(interface)文字不存在

最佳答案

它正在检查 err实现函数Is(error) bool .它等同于您使用命名接口(interface)描述的内容。它不是接口(interface)文字,而是匿名接口(interface)。

关于go - 这种类型断言的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63085138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com