Loading [MathJax]/jax/input/TeX/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >TypeOf与errors.Is的可比性

TypeOf与errors.Is的可比性
EN

Stack Overflow用户
提问于 2022-01-16 08:30:33
回答 1查看 105关注 0票数 1

我正在学习如何在Go中进行错误比较,并发现一些我无法理解的东西。

函数errors.Is(err, target error)检查目标是否具有可比性。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func Is(err, target error) bool {
    if target == nil {
        return err == target
    }

    isComparable := reflectlite.TypeOf(target).Comparable()
    for {
        if isComparable && err == target {
            return true
        }

来源

考虑到Go中的所有接口都是可比较的,而error是一个接口,那么该调用处理哪种情况?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-16 08:38:16

接口值是可比较的,但在运行时,比较可能会引起恐慌。The 说明书上说

如果将两个接口值与相同的动态类型进行比较,则如果该类型的值不可比较,则会引起运行时恐慌。

当目标的具体类型不可比较时,检查通过跳过比较来防止恐慌。

下面是一个例子:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
type E []byte
func (e E) Error() string { return string(e) }
func (e E) Is(target error) bool {
    t, ok := target.(E)
    return ok && bytes.Equal(e, t)
}

var a error = E{}
var b error = E{}
fmt.Println(errors.Is(a, b)) // prints true
fmt.Println(a == b)          // panics because slices are not comparable
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70731980

复制
相关文章
[MethodImpl(MethodImplOptions.Synchronized)]、lock(this)与lock(typeof(...))
对于稍微有点经验的.NET开发人员来说,倘若被问及如何保持线程同步,我想很多人都能说好好几种。在众多的线程同步的可选方式中,加锁无疑是最为常用的。如果仅仅是基于方法级别的线程同步,使用System.Runtime.CompilerServices.MethodImplAttribute无疑是最为简洁的一种方式。MethodImplAttribute可以用于instance method,也可以用于static method。当在某个方法上标注了MethodImplAttribute,并指定MethodImplOptions.Synchronized参数,可以确保在不同线程中运行的该方式以同步的方式运行。我们几天来讨论MethodImplAttribute(MethodImplOptions.Synchronized)和lock的关系。
蒋金楠
2022/05/09
1.3K0
[MethodImpl(MethodImplOptions.Synchronized)]、lock(this)与lock(typeof(...))
Typeof的使用
String类型就是字符串了,空字符串也会返回string,任何加了引号的都是字符串。
wade
2020/04/24
6610
c# typeof 与 GetType 作用与区别
Used to obtain the "System.Type" object for a type. A 'typeof‘ expression takes the following for:
用户2434869
2018/09/12
1.4K0
javascript typeof
if (typeof(temp) == "undefined") { alert("undefined"); } typeof 返回的是字符串,有六种可能: "number"、"string"、"boolean"、"object"、"function"、"undefined" null 表示无值,而 undefined 表示一个未声明的变量,或已声明但没有赋值的变量,或一个并不存在的对象属性。
闵开慧
2018/03/30
8310
[JavaScript]typeof和instanceof的区别
JS里面判断数据类型,一般用typeof或者instanceof两种方法,那么,两者到底有什么区别呢?
娜姐
2022/05/13
8260
[JavaScript]typeof和instanceof的区别
instanceof和typeof的区别
可以看到,对于数字、字符串、布尔值、未定义和空对象,typeof 都返回了相应的类型字符串。但是对于数组和函数,它的返回值都是 "object",因为它们的数据类型都是对象。需要注意的是,如果变量是一个 null 或未声明的变量,typeof 会返回 "undefined"。
执行上下文
2023/09/27
2040
instanceof和typeof的区别
typeof关键字的作用
一、typeof详解: 前言:     typeof关键字是C语言中的一个新扩展,这个特性在linux内核中应用非常广泛。(其实这和C++的auto关键字和可以推断decltype关键字相当类似) 二、实例:       1,把y定义成x指向的数据类型:       typeof(*x) y;    2,把y定义成x指向数据类型的数组:       typeof(*x) y[4];    3,把y定义成一个字符指针数组:               typeof(typeof(char *)[4] y;  
233333
2018/03/07
1K0
typeof 实现原理
typeof (opens new window) 操作符返回一个字符串,表示未经计算的操作数的类型。
Cellinlab
2023/05/17
5840
[JavaScript]js中typeof的用法
1. typeof的语法 typeof是一个运算符,有2种使用方式:typeof(表达式)和typeof 变量名,第一种是对表达式做运算,第二种是对变量做运算。
唯一Chat
2020/03/19
4.8K0
Go 源码解读|如何用好 errors 库的 errors.Is() 与 errors.As() 方法
快一个月没有更新技术文章了,这段时间投注了较多的时间学习字节的开源项目 Kitex/Hertz ,并维护一些简单的 issue ,有兴趣的同学也可以去了解:
白泽z
2022/12/20
9530
javascript当中类型转换,typeof的用法
1)类型转换,typeof的用法 例 3.1.1 <HTML> <head>     <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <BODY> <SCRIPT LANGUAGE="JavaScript">     <!--     /*     Cast operator (Definition)  refer to 过去的网站www.favo.com     A way of converting data types.     Primitive values can be converted from one to another or rendered as objects by using object constructors to convert the values.     Boolean                                            Number                                             String                                             Number() (Function)  马克-to-win: actually Number() is the method of Global object.     A Number type convertor.     Property/method value type: Number primitive     JavaScript syntax: - Number()     - Number(aValue)     Argument list: aValue A value to be converted to a number.     When the Number() constructor is called as a function, it will perform a type conversion.     The following values are yielded as a result of calling Number() as a function:     Value                            Result     Number                            No conversion, the input value is returned unchanged.     Non numeric string                NaN     window.Number("23");在favo中查不出来, 但Idea中可以打点打出来。     */     var a = 9; /*下句话如果放在ie8中执行, 必须打开debug工具*/ //    console.log(typeof(a));     document.writeln(typeof(a));     var as = String(a);     //String是Global的方法     document.writeln("typeof(as) is " + typeof(as));     var x = window.Number("23");     document.writeln("typeof(x) is " + typeof(x));     var age2 = Number("56");     document.writeln(typeof(age2) + "is typeof(age2)");     var age3 = new Number(56);
马克java社区
2019/10/04
7720
javascript当中类型转换,typeof的用法
typeof和instanceof原理
这里的类型值是值,变量是没有类型的,变量可以随时持有任何类型的值。JavaScript中变量是“弱类型”的,一个变量可以现在被赋值为 字符串类型,随后又被赋值为数字类型。
木子星兮
2020/07/16
2.5K0
TS中keyof和typeof
这样固然可以,但是指不定哪天就蹦出来一个undefined,就不能体现出TS的优势了。
kifuan
2022/10/24
1.2K0
js typeof和instanceof 区别
typeof一般是用来判断简单数据类型的,对一个值使用 typeof 操作符会返回下列字符串之一:
IT工作者
2022/05/13
1.6K0
JavaScript typeof, null, 和 undefined
JavaScript typeof, null, undefined, valueOf()。
陈不成i
2021/07/16
8760
TypeScript typeof 操作符
在 TypeScript 中,typeof 操作符可以用来获取一个变量或对象的类型。
阿宝哥
2020/03/12
6.4K0
C语言关键字 typeof 的妙用
typeof() 是GUN C提供的一种特性,它可以取得变量的类型,或者表达式的类型。可以参考:
混说Linux
2022/07/14
1K0
【说站】JavaScript中typeof类型判断的使用
最好使用instanceof。instanceof的原理是基于原型链的查询。只要在原型链中,判断永远是true。
很酷的站长
2022/11/23
7020
【说站】JavaScript中typeof类型判断的使用
typeof最新原理解析
我们都知道 typeof(null) === 'object',关于原因,在小黄书《你不知道的JavaScript》中有这么一段解释:
Jean
2018/11/12
2.6K1
typeof最新原理解析
js typeof 返回类型「建议收藏」
number, boolean, string, undefined, object, function,symbol(ES6以上版本才有);
全栈程序员站长
2022/11/17
2.2K0

相似问题

为什么"typeof“与"typeof()”相同?

33

TypeOf与Select

10

JavaScript hasOwnProperty与typeof

11

Errors.Is(.)不是对称的?

24

Java可比性与TreeSet

13
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文