golang两切片的值比较_比较Go值

golang两切片的值比较

按类型比较 (Comparability by type)

Basic data types are always comparable using the == and != operators: integer values, floating-point numbers, complex numbers, boolean values, string values, constant values.

基本数据类型始终可以使用==!=运算符进行比较: 数值, 浮点数复数布尔值, 字符串值, 量值。

Array values are comparable, if they contain a comparable element type

如果数组值包含可比较的元素类型,则可比较

Pointer values are comparable.

指针值是可比较的。

Channel values are comparable.

通道值是可比较的。

Interface values are comparable.

接口值是可比较的。

Comparing interface values works only if the dynamic type is comparable.

仅当动态类型可比较时,比较接口值才有效。

Function values, Slice values and Map values are not comparable, they can only be compared with nil, as a special case.

函数值, Slice值和Map不可比较,在特殊情况下只能与nil进行比较。

比较规则 (The rules of comparison)

From the Go spec:

从Go规范:

  • Boolean values are comparable. Two boolean values are equal if they are either both true or both false.

    布尔值是可比较的。 如果两个布尔值均为truefalse则相等。

  • Integer values are comparable and ordered, in the usual way.

    数值是通常可比较且有序的。

  • Floating point values are comparable and ordered, as defined by the IEEE-754 standard.

    浮点值是可比较的且有序,如IEEE-754标准所定义。

  • Complex values are comparable. Two complex values u and v are equal if both real(u) == real(v)andimag(u) == imag(v)`.

    数值是可比较的。 如果real(u) == real(v) and imag(u)== imag(v)`,则两个复数值u和v相等。

  • String values are comparable and ordered, lexically byte-wise.

    字符串值是可比较的且按字节顺序排序。

  • Pointer values are comparable. Two pointer values are equal if they point to the same variable or if both have value nil. Pointers to distinct zero-size variables may or may not be equal.

    指针值是可比较的。 如果两个指针值指向相同的变量,或者两个指针的值均为nil则它们相等。 指向不同零大小变量的指针可以相等或可以不相等。

  • Channel values are comparable. Two channel values are equal if they were created by the same call to make or if both have value nil.

    通道值是可比较的。 如果两个通道值是由相同的make调用创建的,或者两个值都为nil则它们是相等的。

  • Interface values are comparable. Two interface values are equal if they have identical dynamic types and equal dynamic values or if both have value nil.

    接口值是可比较的。 如果两个接口值具有相同的动态类型和相等的动态值,或者两个接口值都为nil则它们相等。

  • A value x of non-interface type X and a value t of interface type T are comparable when values of type X are comparable and X implements T. They are equal if t’s dynamic type is identical to X and t’s dynamic value is equal to x.

    当类型X值可比较并且X实现T时, 非接口类型X的值t接口类型T的值t是可比较的。 如果t的动态类型等于X并且t的动态值等于x则它们相等。

  • Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

    如果结构的所有字段都是可比较的,则它们的值是可比较的。 如果两个结构值对应的非空白字段相等,则它们相等。

  • Array values are comparable if values of the array element type are comparable. Two array values are equal if their corresponding elements are equal.

    如果数组元素类型的值可比较,则数组值可比较。 如果两个数组的对应元素相等,则它们相等。

比较失败 (Failing comparing)

If the struct contains a field whose type is not comparable, you’ll get a compile time error when comparing.

如果该结构包含一个类型不可比的字段,则比较时会出现编译时错误。

比较refect.DeepEqual()比较的类型:输入refect.DeepEqual() (Comparing uncomparable types: enter refect.DeepEqual())

The reflect stdlib package provides the reflect.DeepEqual() function which takes 2 types, and returns a boolean:

所述reflect STDLIB包提供reflect.DeepEqual()函数,它接受2种类型,并返回一个布尔值:

func DeepEqual(x, y interface{}) bool

func DeepEqual(x, y interface{}) bool

Values of distinct types are never deeply equal, so if you pass 2 different types you’ll always get false.

不同类型的值永远不会完全相等,因此,如果您传递2个不同类型,则总是会得到false

Array values are deeply equal when their corresponding elements are deeply equal.

当数组的对应元素深度相等时, 数组值深度相等。

Struct values are deeply equal if their corresponding fields, are deeply equal.

如果结构的值对应的字段非常相等,则它们的值非常相等。

Func values are deeply equal if both are nil; otherwise they are not deeply equal.

FUNC值是深层相等如果两者都nil ; 否则,它们就不会完全平等。

Interface values are deeply equal if they hold deeply equal concrete values.

如果接口值具有完全相等的具体值,则它们是高度相等的。

Map values are deeply equal when all of the following are true: - they are both nil or both non-nil - they have the same length - they are the same map object or their corresponding keys (matched using Go equality) map to deeply equal values.

当满足以下所有条件时, 映射值深度相等:-它们都为nil或都为非nil它们具有相同的长度-它们是相同的映射对象或它们的对应键(使用Go相等匹配)映射为深度相等价值观。

Pointer values are deeply equal if they are equal using Go’s == operator or if they point to deeply equal values.

如果指针值使用Go's ==运算符相等,或者它们指向深度相等的值,则它们的深度相等。

Slice values are deeply equal when all of the following are true: - they are both nil or both non-nil - they have the same length, - they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal.

当满足以下所有条件时, 切片值将完全相等:-它们均为nil或都不为nil它们具有相同的长度,-它们指向同一基础数组的相同初始条目(即&x[0] == &y[0] )或它们对应的元素(最大长度)非常相等。

A non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) are not deeply equal.

非nil空片和nil片(例如[]byte{}[]byte(nil) )不完全相等。

Other values - numbers, bools, strings, and channels - are deeply equal if they are equal using Go’s == operator.

如果其他值- 数字布尔值, 字符串通道 -如果使用Go的==运算符相等,则它们将非常相等。

Some inevitable “special” cases worth listing: it is possible for a value to be unequal to itself: - because it is of func type - because it is a floating-point NaN value - because it is an array, struct, or interface containing such a value

某些不可避免的“特殊”情况值得列举: 值可能与自身不相等 :-因为它是func类型的-因为它是浮点NaN值-因为它是包含以下内容的数组结构接口这样的价值

Pointer values are always equal to themselves, even if they point at or contain such problematic values, because they compare equal using Go’s == operator, and that is a sufficient condition to be deeply equal, regardless of content.

指针值即使指向或包含此类有问题的值,也始终等于其自身,因为它们使用Go的==运算符进行相等比较,而这足以使深度相等,而与内容无关。

The same applies to slices and maps: if x and y are the same slice or the same map, they are deeply equal regardless of content.

切片贴图也是如此 :如果xy是相同的切片或相同的贴图,则无论内容如何,​​它们的深度都相等。

翻译自: https://flaviocopes.com/golang-comparing-values/

golang两切片的值比较

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页