1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-06 13:42:28 +08:00
Files
lancet/docs/condition_zh-CN.md
2022-12-31 16:47:18 +08:00

5.2 KiB
Raw Permalink Blame History

Condition

condition包含一些用于条件判断的函数。这个包的实现参考了carlmjohnson的truthy包的实现更多有用的信息可以在truthy中找到感谢carlmjohnson。

源码:

用法:

import (
    "github.com/duke-git/lancet/v2/condition"
)

Index

目录

Bool

返回传入参数的bool值.
如果出入类型参数含有Bool方法, 会调用该方法并返回
如果传入类型参数有IsZero方法, 返回IsZero方法返回值的取反
slices和map的length大于0时返回true否则返回false
其他类型会判断是否是零值

函数签名:

func Bool[T any](value T) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
	// bool
	fmt.Println(condition.Bool(false)) // false
	fmt.Println(condition.Bool(true)) // true

	// integer
	fmt.Println(condition.Bool(0)) // false
	fmt.Println(condition.Bool(1)) // true

	// float
	fmt.Println(condition.Bool(0.0)) // false
	fmt.Println(condition.Bool(0.1)) // true

	// string
	fmt.Println(condition.Bool("")) // false
	fmt.Println(condition.Bool(" ")) // true
	fmt.Println(condition.Bool("0")) // true

	// slice
	var nums [2]int
	fmt.Println(condition.Bool(nums)) // false
	nums = [2]int{0, 1}
	fmt.Println(condition.Bool(nums)) // true

	// map
	fmt.Println(condition.Bool(map[string]string{})) // false
	fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true

	// struct
	fmt.Println(condition.Bool(struct{}{})) // false
	fmt.Println(condition.Bool(time.Now())) // true
}

And

逻辑且操作当切仅当a和b都为true时返回true

函数签名:

func And[T, U any](a T, b U) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
	fmt.Println(condition.And(0, 1)) // false
	fmt.Println(condition.And(0, "")) // false
	fmt.Println(condition.And(0, "0")) // false
	fmt.Println(condition.And(1, "0")) // true
}

Or

逻辑或操作当切仅当a和b都为false时返回false

函数签名:

func Or[T, U any](a T, b U) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
	fmt.Println(condition.Or(0, "")) // false
	fmt.Println(condition.Or(0, 1)) // true
	fmt.Println(condition.Or(0, "0")) // true
	fmt.Println(condition.Or(1, "0")) // true
}

Xor

逻辑异或操作a和b相同返回falsea和b不相同返回true

函数签名:

func Xor[T, U any](a T, b U) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
	fmt.Println(condition.Xor(0, 0)) // false
	fmt.Println(condition.Xor(0, 1)) // true
	fmt.Println(condition.Xor(1, 0)) // true
	fmt.Println(condition.Xor(1, 1)) // false
}

Nor

异或的取反操作

函数签名:

func Nor[T, U any](a T, b U) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
	fmt.Println(condition.Nor(0, 0)) // true
	fmt.Println(condition.Nor(0, 1)) // false
	fmt.Println(condition.Nor(1, 0)) // false
	fmt.Println(condition.Nor(1, 1)) // false
}

Xnor

如果a和b都是真的或a和b均是假的则返回true。

函数签名:

func Xnor[T, U any](a T, b U) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
	fmt.Println(condition.Xnor(0, 0)) // true
	fmt.Println(condition.Xnor(0, 1)) // false
	fmt.Println(condition.Xnor(1, 0)) // false
	fmt.Println(condition.Xnor(1, 1)) // true
}

Nand

如果a和b都为真返回false否则返回true

函数签名:

func Nand[T, U any](a T, b U) bool

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
	fmt.Println(condition.Nand(0, 0)) // true
	fmt.Println(condition.Nand(0, 1)) // true
	fmt.Println(condition.Nand(1, 0)) // true
	fmt.Println(condition.Nand(1, 1)) // false
}

TernaryOperator

三元运算符

函数签名:

func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/condition"
)

func main() {
	trueValue := "1"
	falseValue := "0"

	fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1"
}