Compare Go value

Compare by type

Basic data types can always be combined with==with!=Operator:IntegerValues,floating pointdigital,complicateddigital,Boolean valueValues,StringValues,continuousValues.

Large numbersIf the values contain comparable element types, they are comparable

pointerThe values are comparable.

channelThe values are comparable.

interfaceThe values are comparable.

In comparisoninterfaceThe value is only valid when the dynamic type is comparable.

FeaturesValues,sheetValues andmapValue isIs notComparability, they can only be compared withnil, As a special case.

Comparison rule

From the Go specification:

  • BooleanThe values are comparable. If two boolean values are equal, they are equaltrueOr bothfalse.

  • IntegerThe values are comparable and sorted in the usual way.

  • floating pointValues are comparable and orderly, such asIEEE-754standard.

  • complicatedThe values are comparable. If two complex values u and v are equalreal(u) == real(v)andimag(u) == imag(v)`.

  • StringThe values are comparable and are sorted lexically and in byte order.

  • pointerThe values are comparable. If two pointer values point to the same variable or both have values, they are equalnil. The pointers to different zero-sized variables can be equal or unequal.

  • channelThe values are comparable. If two channel values are created by the same make call, or both channels have values, they are equalnil.

  • interfaceThe values are comparable. If two interface values have the same dynamic type and equal dynamic values, or if both interface values have values, they are equalnil.

  • X valueNon-interfaceTypes ofXAnd a valuetofinterfaceTypes ofTComparable type valueXIs comparableXimplementT. They are equal iftDynamic type withXAnd the dynamic value of t is equal tox.

  • structureIf all of their fields are comparable, the values are comparable. If the non-blank fields corresponding to two structure values are equal, they are equal.

  • Large numbersIf the values of the array element types are comparable, then these values are comparable. If the corresponding elements of the two arrays are equal, they are equal.

Comparison failed

If the structure contains an incomparable field, a compile-time error will occur when comparing.

Comparable incomparable type: inputrefect.DeepEqual()

ThisreflectThe stdlib package providesreflect.DeepEqual()This function accepts two types and returns a Boolean value:

func DeepEqual(x, y interface{}) bool

Different types of values are never exactly equal, so if you pass 2 different types, you will always getfalse.

Large numbersWhen their corresponding elements are equal in depth, the value depth is equal.

structureIf their corresponding fields are equal in depth, the values are equal in depth.

FeaturesIf the two values are equal, the values are exactly equalnil; Otherwise, they will not be completely equal.

interfaceIf they have very equal specific values, then their values are very equal.

mapWhen all of the following conditions are met, the values will be exactly equal:-They are allnilOr neithernil-They have the same length-they are the same map object or its corresponding key (using Go equality for matching) mapped to values of equal depth.

pointerIf Go's values are equal, the values are very equal==Operators, or whether they point to very equal values.

sheetWhen all of the following conditions are met, the values will be exactly equal:-They are allnilOr neithernil-They have the same length,-they point to the same initial entry of the same base array (ie,&x[0] == &y[0]) Or their corresponding elements (maximum length) are very equal.

A non-zero empty slice and anilSlice (for example,[]byte{}with[]byte(nil)) Are not exactly equal.

Other values-digital,Boolean,Strings, withChannel-If you use Go's == operator to be equal, the depths are equal.

Some inevitable "special" cases are worth enumerating:The value may not be equal to itself:-because it isfuncType-because it is a floating point numberNaNValue-because it is aLarge numbers,structure, OrinterfaceContains such a value

Pointer values always point to themselves, even if they point to or contain such problematic values, because they are compared using Go's.==Operators, regardless of content, are enough to be a condition of equal depth.

Also applies toslicewithmap: in casexwithyIt is the same slice or the same map, no matter what the content, they are all equal.


More tutorials: