// DeepEqual reports whether x and y are ``deeply equal,'' defined as follows. // Two values of identical type are deeply equal if one of the following cases applies. // Values of distinct types are never deeply equal. // 条件:数组深度相等,相应的元素都是相等的。 // Array values are deeply equal when their corresponding elements are deeply equal. // 条件:结构体相对应的字段都是相等的。 // Struct values are deeply equal if their corresponding fields, // both exported and unexported, are deeply equal. // 条件:函数都是nil,则为深度相等,其它情况下都是不相等的。 // Func values are deeply equal if both are nil; otherwise they are not deeply equal. // // 条件:两个interface持有深度相同的值。 // Interface values are deeply equal if they hold deeply equal concrete values. // 条件:下面条件为true,则深度相等: 两个全是nil,或者全non-nil,有相同的长度并且有相同的对象/key对应的值是相等的。 // 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, // and either they are the same map object or their corresponding keys // (matched using Go equality) map to deeply equal values. 条件:用 == 比较或者 point的 // Pointer values are deeply equal if they are equal using Go's == operator // or if they point to deeply equal values. 条件:下面条件为true,则深度相等: 两个全是nil,或者全non-nil,有相同的长度,指向相同的初始化节点(即:相同的数组)或相同的元素深度相等。 注意:empty和nil slice不是深度相等的。 // 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, // and either 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. // Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) // are not deeply equal. // // Other values - numbers, bools, strings, and channels - are deeply equal // if they are equal using Go's == operator. // // In general DeepEqual is a recursive relaxation of Go's == operator. // However, this idea is impossible to implement without some inconsistency. // Specifically, it is possible for a value to be unequal to itself, // either because it is of func type (uncomparable in general) // or because it is a floating-point NaN value (not equal to itself in floating-point comparison), // or because it is an array, struct, or interface containing // such a value. // On the other hand, 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. // DeepEqual has been defined so that the same short-cut applies // to slices and maps: if x and y are the same slice or the same map, // they are deeply equal regardless of content. // // As DeepEqual traverses the data values it may find a cycle. The // second and subsequent times that DeepEqual compares two pointer // values that have been compared before, it treats the values as // equal rather than examining the values to which they point. // This ensures that DeepEqual terminates.
..... .... ... .. . 递归次数超过10次则会painc.... // if depth > 10 { panic("deepValueEqual") } // for debugging
// We want to avoid putting more in the visited map than we need to. // For any possible reference cycle that might be encountered, // hard(v1, v2) needs to return true for at least one of the types in the cycle, // and it's safe and valid to get Value's internal pointer. hard := func(v1, v2 Value)bool { switch v1.Kind() { case Map, Slice, Ptr, Interface: // Nil pointers cannot be cyclic. Avoid putting them in the visited map. return !v1.IsNil() && !v2.IsNil() } returnfalse } . .. ... .... ..... ......