compareKit

package
v2.8.131 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Diff func(x, y interface{}, opts ...cmp.Option) string = cmp.Diff

Diff 获取差异.

@return 如果为""则说明两个传参一致.

View Source
var Equal func(x, y interface{}, opts ...cmp.Option) bool = cmp.Equal

Equal 是否相等?

!!!: (1) 如果传参结构体(或其内部的结构体)实现了 "(T) Equal(T) bool" 或者 "(T) Equal(I) bool",

且方法的receiver必须为 "值类型",这样的话,无论比较 结构体实例 还是 结构体实例指针 都可以.

(2) 如果传参x、y都是 非nil的结构体实例指针,比较的是它们的内容是否相等.

PS: (1) 如果传参分别为 结构体实例 和 结构体实例指针,将返回false. (2) 传参x、y值都为nil的情况下, (a) 类型不同,将返回 false;

(b) 类型相同,将返回 true.

e.g.

type person struct {
	Name string
	Age  int
}

p1 := &person{"Alice", 18}
p2 := &person{"Bob", 20}
p3 := &person{"Alice", 18}

fmt.Println(compareKit.Equal(p1, p2)) // false
fmt.Println(compareKit.Equal(p1, p3)) // true
View Source
var Equal1 func(x, y any) bool = reflect.DeepEqual

Equal1 比较2个结构体实例指针的内容是否相等.

e.g.

type person struct {
	Name string
	Age  int
}

p1 := &person{"Alice", 18}
p2 := &person{"Bob", 20}
p3 := &person{"Alice", 18}

fmt.Println(reflect.DeepEqual(p1, p2)) // false
fmt.Println(reflect.DeepEqual(p1, p3)) // true

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL