uts

package module
v0.0.0-...-a1699f1 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 15 Imported by: 0

README

数组处理

函数 ArrConcat

ArrConcat 是一个用于合并两个或多个数组的函数,返回一个新的数组,其中包含原始数组中的所有元素。该函数的类型参数 V 可以表示任何类型的数组元素。

func ArrConcat[V any](arr []V, arrs ...[]V) []V
参数
  • arr:要连接的第一个数组。
  • arrs:可变参数,要连接的后续数组。
返回值
  • 一个新的数组,其中包含原始数组中的所有元素。
示例
package main

import (
	"fmt"
)

func main() {
	arr1 := []int{1, 2, 3}
	arr2 := []int{4, 5, 6}
	arr3 := []int{7, 8, 9}

	result := ArrConcat(arr1, arr2, arr3)
	fmt.Println(result) // 输出:[1 2 3 4 5 6 7 8 9]
}
类型参数 V

V 是一个类型参数,表示要连接的数组中的元素类型。类型参数可以表示任何类型的数组元素,例如 intstringfloat64 等。

示例
package main

import (
	"fmt"
)

func main() {
	arr1 := []string{"apple", "banana", "cherry"}
	arr2 := []string{"orange", "grape", "strawberry"}

	result := ArrConcat(arr1, arr2)
	fmt.Println(result) // 输出:["apple", "banana", "cherry", "orange", "grape", "strawberry"]
}
函数 ArrFind

ArrFind 是一个用于在数组中查找特定元素的函数,返回一个布尔值,表示元素是否存在于数组中。该函数的类型参数 V 可以表示任何类型的数组元素。

func ArrFind[V any](arr []V, value V) bool
参数
  • arr:要搜索的数组。
  • value:要查找的元素。
返回值
  • 一个布尔值,表示元素是否存在于数组中。
示例
package main

import (
	"fmt"
)

func main() {
	arr := []int{1, 2, 3, 4, 5}

	result := ArrFind(arr, 3)
	fmt.Println(result) // 输出:true

	result = ArrFind(arr, 6)
	fmt.Println(result) // 输出:false
}
类型参数 V

V 是一个类型参数,表示要查找的数组中的元素类型。类型参数可以表示任何类型的数组元素,例如 intstringfloat64 等。

示例
package main

import (
	"fmt"
)

func main() {
	arr := []string{"apple", "banana", "cherry", "orange", "grape"}

	result := ArrFind(arr, "banana")
	fmt.Println(result) // 输出:true

	result = ArrFind(arr, "kiwi")
	fmt.Println(result) // 输出:false
}

这是一个关于 ArrFilter 函数的 Markdown 文档:

函数 ArrFilter

ArrFilter 是一个用于从数组中过滤元素的函数,返回一个新的数组,其中包含满足特定条件的元素。该函数的类型参数 V 可以表示任何类型的数组元素。

func ArrFilter[V any](arr []V, predicate func(V) bool) []V
参数
  • arr:要过滤的数组。
  • predicate:一个函数,用于判断元素是否满足条件。该函数接受一个参数 V,并返回一个布尔值。
返回值
  • 一个新的数组,其中包含满足特定条件的元素。
示例
package main

import (
	"fmt"
)

func main() {
	arr := []int{1, 2, 3, 4, 5}

	// 过滤出偶数
	evenArr := ArrFilter(arr, func(x int) bool {
		return x%2 == 0
	})
	fmt.Println(evenArr) // 输出:[2 4 6]

	// 过滤出大于 3 的数
	greaterThanThreeArr := ArrFilter(arr, func(x int) bool {
		return x > 3
	})
	fmt.Println(greaterThanThreeArr) // 输出:[4 5]
}
类型参数 V

V 是一个类型参数,表示要过滤的数组中的元素类型。类型参数可以表示任何类型的数组元素,例如 intstringfloat64 等。

示例
package main

import (
	"fmt"
)

func main() {
	arr := []string{"apple", "banana", "cherry", "orange", "grape"}

	// 过滤出以字母 "a" 开头的字符串
	startWithAArr := ArrFilter(arr, func(x string) bool {
		return strings.HasPrefix(x, "a")
	})
	fmt.Println(startWithAArr) // 输出:["apple", "banana"]
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrChunk

func ArrChunk[V any](arr []V, sizes ...int) [][]V

ArrChunk 将数组按指定size分组,若不能均分,最后一组包含剩下的元素

Example
fmt.Println(ArrChunk([]int{1, 2, 3, 4}, 3))
Output:

[[1 2 3] [4]]

func ArrConcat

func ArrConcat[V any](arr []V, arrs ...[]V) []V

ArrConcat 将多个数组的元素全部拼接到一个新数组并返回

Example
fmt.Println(ArrConcat([]int{1}))
fmt.Println(ArrConcat([]int{1}, []int{2, 3}, []int{4}))
Output:

[1]
[1 2 3 4]

func ArrCount

func ArrCount[V comparable](arr []V, vals ...V) int

ArrCount 返回数组中满足指定条件的元素个数

Example
fmt.Println(ArrCount([]int{1, 2, 2, 2, 3}, 2))
Output:

3

func ArrEvery

func ArrEvery[V any, T []V](arr T, fn func(val V, idx int) bool) bool

ArrEvery 判断数组中是否所有元素都满足条件,首次不满足时结束

Example
fmt.Println(ArrEvery([]int{1, 2, 3, 4}, func(val int, idx int) bool {
	return val > 2
}))
Output:

false

func ArrFill

func ArrFill[V any, T []V](arr *T, val V) *T

ArrFill 将数组元素全部填充为指定值

Example
fmt.Println(ArrFill(&[]int{1, 2, 3}, 1))
fmt.Println(ArrFill(&[]int{1, 2, 3, 4}, 2))
Output:

&[1 1 1]
&[2 2 2 2]

func ArrFilter

func ArrFilter[V any, T []V](arr T, fn func(val V, idx int) bool) T

ArrFilter 筛选数组元素,剔除不满足fn()==true的元素

Example
fmt.Println(ArrFilter([]int{1, 2, 3, 4}, func(v int, i int) bool {
	return v%2 == 0
}))
Output:

[2 4]

func ArrFind

func ArrFind[V any, T []V](arr T, fn func(val V, idx int) bool) V

ArrFind 查找数组中满足条件的元素,找到时会提前结束,并返回该元素

Example
fmt.Println(ArrFind([]int{1, 2, 3, 4}, func(val int, idx int) bool {
	return val%2 == 0
}))
Output:

2

func ArrFindIndex

func ArrFindIndex[V any, T []V](arr T, fn func(val V, idx int) bool) int

ArrFindIndex 查找数组中满足条件的元素,找到时会提前结束,并返回该元素下标,否则返回-1

Example
fmt.Println(ArrFindIndex([]int{1, 2, 3, 4}, func(val int, idx int) bool {
	return val%2 == 0
}))
Output:

1

func ArrFindLast

func ArrFindLast[V any, T []V](arr T, fn func(val V, idx int) bool) V

ArrFindLast 倒序查找数组中满足条件的元素,找到时会提前结束,并返回该元素

Example
fmt.Println(ArrFindLast([]string{"a", "ab", "abc", "bc"}, func(val string, idx int) bool {
	return strings.Contains(val, "a")
}))
Output:

abc

func ArrFindLastIndex

func ArrFindLastIndex[V any, T []V](arr T, fn func(val V, idx int) bool) int

ArrFindLastIndex 倒序查找数组中满足条件的元素,找到时会提前结束,并返回该元素下标,否则返回-1

Example
fmt.Println(ArrFindLastIndex([]string{"a", "ab", "abc", "bc"}, func(val string, idx int) bool {
	return strings.Contains(val, "a")
}))
Output:

2

func ArrIncludes

func ArrIncludes[V comparable, T []V](arr T, val V) bool

ArrIncludes 按指定条件查找元素,存在时返回true,否则返回false

Example
fmt.Println(ArrIncludes([]int{1, 2, 3, 4}, 3))
fmt.Println(ArrIncludes([]int{1, 2, 3, 4}, 5))
Output:

true
false

func ArrIndexOf

func ArrIndexOf[V comparable, T []V](arr T, val V) int

ArrIndexOf 按指定条件查找元素,存在时返回下标,否则返回-1

Example
fmt.Println(ArrIndexOf([]int{1, 2, 3, 4}, 3))
Output:

2

func ArrJoin

func ArrJoin[V any, T []V, R any](arr T, sep R) string

ArrJoin 将数组以指定分隔符拼接为字符串

Example
arr := []int{1, 2, 3, 4}
fmt.Println(ArrJoin(arr, ","))
Output:

1,2,3,4

func ArrMap

func ArrMap[V any, R any](arr []V, fn func(val V, idx int) R) []R

ArrMap 重构数组元素,新元素为fn()的返回值

Example
fmt.Println(ArrMap([]int{1, 2, 3, 4}, func(v int, i int) int {
	return v * 2
}))
Output:

[2 4 6 8]

func ArrMean

func ArrMean[V Numeric](arr []V) float64

ArrMean 数组元素的平均值

Example
fmt.Println(ArrMean([]int{1, 2, 3, 4}))
Output:

2.5

func ArrPop

func ArrPop[V any, T []V](arr *T) V

ArrPop 从数组中移除并返回最后一个元素

Example
arr := []int{1, 2, 3, 4}
fmt.Println(ArrPop(&arr), arr)
Output:

4 [1 2 3]

func ArrProduct

func ArrProduct[V Numeric](arr []V) V

ArrProduct 数组元素的乘积

Example
fmt.Println(ArrProduct([]int{1, 2, 3, 4}))
Output:

24

func ArrPush

func ArrPush[V any, T []V](arr *T, vals ...V) int

ArrPush 添加一些元素到数组末尾,然后返回现有的元素个数

Example
arr := []int{1, 2, 3, 4}
fmt.Println(ArrPush(&arr, 5), arr)
Output:

5 [1 2 3 4 5]

func ArrReduce

func ArrReduce[V any, R any](arr []V, fn func(prev R, curr V, idx int, arr []V) R, initVal ...R) R

ArrReduce 对数组中的每个元素按序执行fn(),fn会将上次执行结果(prev)作为参数传入,直到最后一次执行并返回结果。

Example
// 数组求和
fmt.Println(ArrReduce([]int{1, 2, 3}, func(prev int, curr int, idx int, arr []int) int {
	return prev + curr
}))
// 元素拼接
fmt.Println(ArrReduce([]int{1, 2, 3}, func(prev string, curr int, idx int, arr []int) string {
	return prev + Str(curr)
}))
Output:

6
123

func ArrSample

func ArrSample[V any](arr []V, count ...int) []V

ArrSample 从数组中随机选取指定数量元素

  • 同一元素不会多次选取,但选取的多个元素可能具有相同值
Example
fmt.Println(ArrSample([]int{1, 2, 3}, 1))
fmt.Println(ArrSample([]int{1, 2, 2, 2, 3}, 3))
Output:

[1]
[3 2 2]

func ArrSampleOne

func ArrSampleOne[V any](arr []V) V

ArrSampleOne 从数组中随机选取1个元素

Example
fmt.Println(ArrSampleOne([]int{1, 2, 3}))
Output:

2

func ArrShift

func ArrShift[V any, T []V](arr *T) V

ArrShift 从数组中移除并返回第一个元素

Example
arr := []int{1, 2, 3, 4}
fmt.Println(ArrShift(&arr), arr)
Output:

1 [2 3 4]

func ArrShuffle

func ArrShuffle[V any](arr []V) []V

ArrShuffle 将数组元素打乱顺序

Example
fmt.Println(ArrShuffle([]int{1, 2, 3}))
Output:

[2 1 3]

func ArrSome

func ArrSome[V any, T []V](arr T, fn func(val V, idx int) bool) bool

ArrSome 判断数组中是否有一个元素满足条件,首次满足时结束

Example
fmt.Println(ArrSome([]int{1, 2, 3, 4}, func(val int, idx int) bool {
	return val > 2
}))
Output:

true

func ArrSplice

func ArrSplice[V any, T []V](arr *T, start, length int, val ...V) T

ArrSplice 从数组start位置开始,删去length个元素,并将val依次拼接至末尾,然后返回被删除的元素

  • 若start为负数,则从末尾开始算,如:-5表示从倒数第5个元素开始
Example
arr := []int{1, 2, 3, 4}
fmt.Println(ArrSplice(&arr, 1, 2, 5, 6), arr)
arr2 := []int{1, 2, 3, 4}
fmt.Println(ArrSplice(&arr2, -4, 3, 5, 6), arr2)
Output:

[2 3] [1 4 5 6]
[1 2 3] [4 5 6]

func ArrSum

func ArrSum[V Numeric](arr []V) V

ArrSum 数组元素求和

Example
fmt.Println(ArrSum([]int{1, 2, 3}))
Output:

6

func ArrUnShift

func ArrUnShift[V any, T []V](arr *T, vals ...V) int

ArrUnShift 添加一些元素到数组的开头,然后返回现有的元素个数

Example
arr := []int{1, 2, 3, 4}
fmt.Println(ArrUnShift(&arr, 0), arr)
Output:

5 [0 1 2 3 4]

func ArrUnion

func ArrUnion[V comparable](arr []V, arrs ...[]V) []V

ArrUnion 将多个数组的元素合并到一个数组,并返回该数组去重后的新数组

Example
fmt.Println(ArrUnion([]int{1, 2, 3, 4}, []int{2, 3, 4}))
Output:

[1 2 3 4]

func ArrUniq

func ArrUniq[V comparable](arr []V) []V

ArrUniq 数组元素去重

Example
fmt.Println(ArrUniq([]int{1, 2, 2, 3}))
Output:

[1 2 3]

func Decode

func Decode[T comparable](val T, matches ...T) (v T)

Decode 类似oracle的decode、php8的match

  • 参数为两个一组,依次往后匹配
  • 1. 若某组的首个值与val相等,则返回该组第2个值
  • 2. 若某组仅1个值,则直接返回该值
  • 3. 否则返回与val相同类型的空值

func F32

func F32(str string) (f32 float32)

F32 转float32

Example
fmt.Printf("%v %T", F32("56.12345678"), F32("56.12345678"))
Output:

56.123455 float32

func F64

func F64(str string) (f64 float64)

F64 转float64

Example
fmt.Printf("%v %T", F64("56.12345678"), F64("56.12345678"))
Output:

56.12345678 float64

func HmacMd5

func HmacMd5(key, data []byte) string

HmacMd5 []byte转md5

Example
fmt.Println(HmacMd5([]byte("123456"), []byte("123456")))
Output:

30ce71a73bdd908c3955a90e8f7429ef

func HmacSha1

func HmacSha1(secret, data []byte) string

HmacSha1 HmacSha1加密

func HmacSha256

func HmacSha256(secret, data []byte) string

HmacSha256 HmacSha256加密

Example
fmt.Println(HmacSha256([]byte("123456"), []byte("123456")))
Output:

b8ad08a3a547e35829b821b75370301dd8c4b06bdd7771f9b541a75914068718

func IIf

func IIf[T any](condition bool, ifTrue, ifFalse T) T

IIf 三元:当条件为true时,返回ifTrue,否则返回ifFalse

func JSON

func JSON(data any) string

JSON 对json数据进行格式化(带缩进)

Example
m := map[string]int{"a": 1, "b": 2}
fmt.Println(JSON(m))
Output:

{
    "a": 1,
    "b": 2
}

func JSONStr

func JSONStr(data any) string

JSONStr 对json数据进行格式化(不带缩进)

func Join

func Join[V any, T []V, R any](arr T, sep R) string

Join 将数组以指定分隔符拼接为字符串

  • ArrJoin的别名
Example
arr := []int{1, 2, 3, 4}
fmt.Println(Join(arr, ","))
Output:

1,2,3,4

func Md5

func Md5(b []byte) string

Md5 []byte转md5

Example
fmt.Println(Md5([]byte("123456")))
Output:

e10adc3949ba59abbe56e057f20f883e

func NewErr

func NewErr(strs ...any) error

NewErr 生成 error ,多个str以":"分隔

func Now

func Now() string

Now 返回当时日期,格式:2006-01-02 15:04:05

Example
fmt.Println(Now())
Output:

2023-12-01 16:45:12

func Nvl

func Nvl[T comparable](val T, vals ...T) T

Nvl 返回参数中第1个不为空的值

  • 类似oracle的nvl

func PrintJSON

func PrintJSON(data any) (j string)

PrintJSON 对json数据进行格式化(带缩进),并打印

Example
m := map[string]int{"a": 1, "b": 2}
PrintJSON(m)
Output:

{
    "a": 1,
    "b": 2
}

func RandChar

func RandChar(n int) (chars string)

RandChar 生成指定长度的大/小写字母和数字的字符串

Example
fmt.Println(RandChar(32))
Output:

RtoNbkXsotPMYQwQHOBVkP57mrOtVnKF

func RandHex

func RandHex(n int) (hexs string)

RandHex 生成指定长度的hex字符串

Example
fmt.Println(RandHex(32))
Output:

070cf8a43e8d63e76b6a5732e681b7bc

func RandI64

func RandI64(min, max int64) int64

RandI64 生成指定范围内的随机数

Example
fmt.Println(RandI64(10, 20))
Output:

15

func RandIMEI

func RandIMEI() (imei string)

RandIMEI 生成随机的手机IMEI串号

Example
fmt.Println(RandIMEI())
Output:

752700536508331

func RandInt

func RandInt(min, max int) int

RandInt 生成指定范围内的随机数

Example
fmt.Println(RandInt(10, 20))
Output:

15

func RandLetter

func RandLetter(n int) (chars string)

RandLetter 生成指定长度的大/小写字母的字符串

Example
fmt.Println(RandLetter(32))
Output:

RJdQgtwErdmfeieKSSFSihuvkJrKTdTM

func RandNum

func RandNum(n int) (num string)

RandNum 生成指定长度的数字字符串

Example
fmt.Println(RandNum(32))
Output:

83420453871701843678066285134205

func Sha1

func Sha1(b []byte) string

Sha1 Sha1加密

Example
fmt.Println(Sha1([]byte("123456")))
Output:

7c4a8d09ca3762af61e59520943dc26494f8941b

func Sha256

func Sha256(b []byte) string

Sha256 Sha256加密

Example
fmt.Println(Sha256([]byte("123456")))
Output:

8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92

func Space

func Space(n int) string

Space 返回指定数量的空格

func Str

func Str(v any) string

Str 转为string

func Trim

func Trim(str string, cutset ...string) string

Trim 清除字符串首尾的指定内容

  • 默认cutest `[\s\t\r\n]+`
Example
str := "\t\r\n a \t\r\n"
fmt.Println(Trim(str), len(str), len(Trim(str)))
Output:

a 9 1

func UUID

func UUID() string

UUID 生成UUID

Example
fmt.Println(UUID())
Output:

7ecc6787-55e1-48f7-a8f5-24bbd5326920

Types

type Numeric

type Numeric interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64
}

Jump to

Keyboard shortcuts

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