gofunctools

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: MIT Imports: 11 Imported by: 0

README

go-functools

Go 写 CRUD 的一些工具函数

Documentation

Index

Constants

This section is empty.

Variables

Functions

func AndSlice

func AndSlice[T comparable](a []T, b []T) []T

AndSlice 对两个 Slice 取交集,时间复杂度为O(max(N, M)),N为a的长度,M为b的长度

func Bind2First

func Bind2First[T1 any, T2 any, U any](f func(T1, T2) U, a1 T1) func(T2) U

func BindFirst

func BindFirst[T any, U any](f func(T) U, a1 T) func() U

func Compose

func Compose[T any, U any, V any](fun1 func(T) U, fun2 func(U) V) func(T) V

func ConvertSameType

func ConvertSameType[R any](data interface{}, typeOf R) (R, bool)

func CopyURLValues

func CopyURLValues(src url.Values) url.Values

深拷贝,string slice 也会拷贝一份新的

func DecryptDES

func DecryptDES(src []byte, key []byte) []byte

DecryptDES DES 解密

func EncryptDES

func EncryptDES(src []byte, key []byte) []byte

EncryptDES DES 加密

func Filter

func Filter[T any](data []T, fun func(T) bool) []T

func Find

func Find[T comparable, U any](key T, data []U, keyFunc func(U) T) (int, bool)

Find 使用 == 作为谓词 的 FindIf

func FindIf

func FindIf[T any, U any](key T, data []U, predicate func(T, U) bool) (int, bool)

FindIf 在 data 里找key,找到返回(下标, true),没找到返回(0, false)

func FoldR

func FoldR[T any, U any](init U, fun func(U, T) U, data ...T) U

func Foldl

func Foldl[T any, U any](init U, fun func(U, T) U, data ...T) U

func GenGetFieldFunc

func GenGetFieldFunc[D, R any](field interface{}) func(D) R

GenGetFieldFunc D 数据类型 R 返回值类型 只支持 struct 和 map,不支持嵌套

func Greater

func Greater[T Ordered](a, b T) bool

func Identity

func Identity[T any](value T) T

Identity 接到什么返什么

func InterfaceToType

func InterfaceToType[T any](v interface{}, typeof T) T

func IsSliceEq

func IsSliceEq[T any, U any](srcs []T, targets []U, eqFun func(src T, tgt U) bool) bool

func Less

func Less[T Ordered](a, b T) bool

func Map

func Map[T any, U any](data []T, fun func(T) U) []U

func Max

func Max[T any](lessFunc func(a, b T) bool, data ...T) T

func Min

func Min[T any](lessFunc func(a, b T) bool, data ...T) T

func OrSlice added in v1.2.0

func OrSlice[T comparable](a []T, b []T) []T

OrSlice 对两个 Slice 取并集,时间复杂度为O(N+M),N为a的长度,M为b的长度

func ParseFormReq added in v1.1.0

func ParseFormReq[T any](value url.Values, parsedReq *T) error

ParseFormReq 从 req 中提取

func PhoneMasking

func PhoneMasking(phone string) string

PhoneMasking 电话脱敏,前后保留两位,其他全部省略

func PtrDeref

func PtrDeref[T any](t *T) T

func Reduce

func Reduce[T any, U any](data []T, fun func(U, T) U, init U) U

func ReverseParamter

func ReverseParamter[T1 any, T2 any, U any](f func(T1, T2) U) func(T2, T1) U

func ReverseParamterWithError

func ReverseParamterWithError[T1 any, T2 any, U any](f func(T1, T2) (U, error)) func(T2, T1) (U, error)

func StringToInt

func StringToInt(s string) int64

StringToInt 有 error 直接 panic

func StructToMap

func StructToMap[T any](s T) map[string]interface{}

StructToMap 将结构体转为 map

func StructToMapByTag

func StructToMapByTag(value interface{}, tag string) map[string]interface{}

func SubSlice

func SubSlice[T comparable](a []T, b []T) []T

SubSlice 两个 Slice 求差

func TmpPtr

func TmpPtr[T any](data T) *T

TmpPtr 字面值变指针

func Unique

func Unique[T comparable](data []T) []T

Unique Slice 去重,O(N) 的复杂度

func UniqueWithEqFun

func UniqueWithEqFun[T any](data []T, eqFun func(T, T) bool) []T

UniqueWithEqFun 对 slice 去重,O(N^2) 的复杂度,数据长度 > 1000 时慎用

func WrapWithLabel

func WrapWithLabel(data []string, leftLabel string, rightLabel string) string

Types

type Float

type Float interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered

type Ordered interface {
	Integer | Float | ~string
}

type RWLockDataWrapper

type RWLockDataWrapper[T any] struct {
	// contains filtered or unexported fields
}

RWLockDataWrapper 包了一层读写锁的变量 别传指针或按引用

func (*RWLockDataWrapper[T]) Read

func (w *RWLockDataWrapper[T]) Read() T

func (*RWLockDataWrapper[T]) Write

func (w *RWLockDataWrapper[T]) Write(data T)

type Tuple

type Tuple[T, U any] struct {
	First  T
	Second U
}

func Zip

func Zip[T, U any](s1 []T, s2 []U) []Tuple[T, U]

Jump to

Keyboard shortcuts

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