compareKit

package
v3.0.13 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// EqualWithTypeAndValue 检查两个值是否相等(检查类型和值).
	EqualWithTypeAndValue func(left, right any) bool = compare.Equal

	// EqualWithValue 检查两个值是否相等(只检查值).
	EqualWithValue func(left, right any) bool = compare.EqualValue

	// LessThan 验证参数`left`的值是否小于参数`right`的值.
	LessThan func(left, right any) bool = compare.LessThan

	// GreaterThan 验证参数`left`的值是否大于参数`right`的值.
	GreaterThan func(left, right any) bool = compare.GreaterThan

	// LessOrEqual 验证参数`left`的值是否小于或等于参数`right`的值.
	LessOrEqual func(left, right any) bool = compare.LessOrEqual

	// GreaterOrEqual 验证参数`left`的值是否大于或参数`right`的值.
	GreaterOrEqual func(left, right any) bool = compare.GreaterOrEqual
)
View Source
var Diff func(x, y interface{}, opts ...cmp.Option) string = cmp.Diff

Diff 获取差异.

!!!: (0) 可能导致"卡死"(可能是死循环)的情况: 结构体内部的结构体实现了"(T) EqualWithTypeAndValue(T) bool" 或者 "(T) EqualWithTypeAndValue(I) bool";

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

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

Equal 是否相等?

!!!: (0) 可能导致"卡死"(可能是死循环)的情况: 结构体内部的结构体实现了"(T) EqualWithTypeAndValue(T) bool" 或者 "(T) EqualWithTypeAndValue(I) bool"; (1) 如果传参结构体(或其内部的结构体)实现了 "(T) EqualWithTypeAndValue(T) bool" 或者 "(T) EqualWithTypeAndValue(I) bool",

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

PS: (1) 传参x、y都是 非nil的结构体实例指针,比较的是它们的内容是否相等; (2) 传参x、y都是 切片实例,两者内容相同且顺序相同,将返回true;否则返回false; (3) 如果传参分别为 结构体实例 和 结构体实例指针,将返回false; (4) 传参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.EqualWithTypeAndValue(p1, p2)) // false
fmt.Println(compareKit.EqualWithTypeAndValue(p1, p3)) // true
View Source
var Equal2 func(x, y any) bool = reflect.DeepEqual

Equal2 通过 reflect标准库 实现,比较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

func InDelta

func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool

InDelta 检查增量内两个值是否相等.

Types

This section is empty.

Jump to

Keyboard shortcuts

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