pts

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: MIT Imports: 18 Imported by: 3

README

pts

介绍

自用工具方法

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrConcat

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

ArrConcat 将多个数组的元素拼接到一个数组

func ArrEvery

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

ArrEvery 循环数组,并在fn()返回false时提前结束循环

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

true
false

func ArrFilter

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

ArrFilter 元素筛选,fn()返回true的元素保留

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

[2 4]

func ArrFind

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

ArrFind 查找元素:返回fn()首次为true的元素,否则返回

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

2

func ArrIncludes

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

ArrIncludes 判断数组是否包含指定元素

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

false
true

func ArrJoin

func ArrJoin(arr []any, sep any) string

ArrJoin 将数组元素转为string,为使用sep拼接

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

1x2x3x4x5

func ArrMap

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

ArrMap 元素重构:使用fn()的返回值替换原值

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

[2 4 6 8 10]

func ArrPop

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

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

- 无可移除元素时返回指定类型的空值

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

5
[1 2 3 4]

func ArrShift

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

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

- 无可移除元素时返回指定类型的空值

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

1
[2 3 4 5]

func ArrSome

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

ArrSome 循环数组,并在fn()返回true时提前结束循环

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

true
false

func ArrSplice

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

ArrSplice 从数组arr的起始位置start开始,删去length个元素,并将val拼接至最后,返回删除的元素切片

- 若start为负数,则从尾部开始算,如:-5,表示从倒数第5个元素开始

- 此方法传入的arr为指针(*T),会改变原数组

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

[3 4]
[1 2 5 7 8 9]

func Decode

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

Decode 类似oracle的decode、php8的match

- 参数为两个一组,依次往后匹配

1. 若某组的首个值与val相等,则返回该组第2个值

2. 若某组仅1个值,则直接返回该值

3. 否则返回与val相同类型的空值

Example
fmt.Println(Decode(5, 1, 10, 5, 50))
fmt.Println(Decode(5, 1, 10, 3, 30))
fmt.Println(Decode(5, 1, 10, 3, 30, 25))
fmt.Println(Decode("5", "1", "10", "2", "20"))
fmt.Println(Decode("5", "1", "10", "5", "20"))
Output:

50
0
25

20

func HmacSha256

func HmacSha256(secret, data []byte) string

HmacSha256 HmacSha256加密

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

4718cebf3bde5716605b6fea813757e05dc234ee47bb6c81b7559d4731949771

func HmacSha256Any

func HmacSha256Any(secret, data any) string

HmacSha256Any HmacSha256加密

Example
var s = struct {
	Name  string
	Phone string
}{
	"Teval", "15656565656",
}
fmt.Println(HmacSha256Any([]byte("key"), s))
fmt.Println(HmacSha256Any([]byte("key"), fmt.Sprintf(`%+v`, s)))
fmt.Println(fmt.Sprintf(`%+v`, s))
Output:

c764cb7f3cebe567e98bc70b8a30f9a7b6d2d36112324abee4475fbd6756ef4e
c764cb7f3cebe567e98bc70b8a30f9a7b6d2d36112324abee4475fbd6756ef4e
{Name:Teval Phone:15656565656}

func HmacSha256Str

func HmacSha256Str(secret, data string) string

HmacSha256Str HmacSha256加密

Example
fmt.Println(HmacSha256Str("key", "abcdefg"))
Output:

4718cebf3bde5716605b6fea813757e05dc234ee47bb6c81b7559d4731949771

func IIf

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

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

Example
v1 := IIf(true, 5, 1)
fmt.Println(fmt.Sprintf(`%v %T`, v1, v1))

v2 := IIf(1 > 2, "5", "1")
fmt.Println(fmt.Sprintf(`%v %T`, v2, v2))
Output:

5 int
1 string

func JSON

func JSON(data any) string

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

Example
var s = struct {
	Name  string `json:"name"`
	Phone string `json:"phone"`
}{
	"Teval", "15656565656",
}
fmt.Println(JSON(s))
Output:

{
    "name": "Teval",
    "phone": "15656565656"
}

func Join

func Join(arr []any, sep any) string

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

Example
arr := []interface{}{1, 2, 3, 4, 5}
fmt.Println(Join(arr, "x"))
Output:

1x2x3x4x5

func Max

func Max[T interface{ Numeric | string }](val T, vals ...T) T

Max 返回参数中的最大值(至少需要1个参数)

Example
fmt.Println(Max(1, 2, 3, 4, 5, 4, 3, 2, 1))
fmt.Println(Max("a", "b", "999"))
fmt.Println(Max("18", "179"))
fmt.Println(Max(1))
Output:

5
b
18
1

func Md5

func Md5(b []byte) string

Md5 []byte转md5

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

7ac66c0f148de9519b8bd264312c4d64

func Md5Any

func Md5Any(str any) string

Md5Any any转md5

Example
var s = struct {
	Name  string
	Phone string
}{
	"Teval", "15656565656",
}
fmt.Println(Md5Any(s))
fmt.Println(Md5Any(fmt.Sprintf(`%+v`, s)))
fmt.Println(fmt.Sprintf(`%+v`, s))
Output:

29e1f5c14d500cdc87d7543e07acd9de
29e1f5c14d500cdc87d7543e07acd9de
{Name:Teval Phone:15656565656}

func Md5Str

func Md5Str(str string) string

Md5Str string转md5

Example
fmt.Println(Md5Str("abcdefg"))
Output:

7ac66c0f148de9519b8bd264312c4d64

func Min

func Min[T interface{ Numeric | string }](val T, vals ...T) T

Min 返回参数中的最小值(至少需要1个参数)

Example
fmt.Println(Min(1, 2, 3, 4, 5, 4, 3, 2, 1))
fmt.Println(Min("a", "b", "999"))
fmt.Println(Min("18", "179"))
fmt.Println(Min("1"))
Output:

1
999
179
1

func NewErr

func NewErr(strs ...any) error

NewErr 生成error,多个参数以:分隔

Example
err := NewErr("假装有个错误")
fmt.Println(fmt.Sprintf(`%v %T`, err, err))
Output:

假装有个错误 *errors.errorString

func Nvl

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

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

- 类似oracle的nvl

Example
fmt.Println(Nvl(""))
fmt.Println(Nvl("", "a"))
fmt.Println(Nvl("", "", "b", "c"))
fmt.Println(Nvl(0, 1, 2))
fmt.Println(Nvl(5, 1, 2))
Output:


a
b
1
5

func PrintBlue

func PrintBlue(msg ...any)

PrintBlue 输出蓝色字体,多个参数以\t分隔

func PrintColor

func PrintColor(attr color.Attribute, msg ...any)

PrintColor 输出指定颜色字体,多个参数以\t分隔

Example
PrintColor(color.FgHiMagenta, "abc", "def")
fmt.Println("abc\tdef")
Output:

abc	def

func PrintGreen

func PrintGreen(msg ...any)

PrintGreen 输出绿色字体,多个参数以\t分隔

func PrintJSON

func PrintJSON(data any) (j string)

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

func PrintRed

func PrintRed(msg ...any)

PrintRed 输出红色字体,多个参数以\t分隔

Example
PrintRed("error")
Output:

func RandIMEI

func RandIMEI() string

RandIMEI 生成随机的手机IMEI串号

Example
fmt.Println(RandIMEI())
Output:

254376154169509

func RandInt

func RandInt(min, max int) int

RandInt 随机生成指定大小范围的整数

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

10

func RandKey

func RandKey(num int) string

RandKey 生成指定长度的字符串

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

a6c0c22f130ac319bfa81f1a79de5c91

func RandLocation

func RandLocation() (lat float64, lon float64)

RandLocation 生成国内随机经纬度

Example
fmt.Println(RandLocation())
Output:

44.1285743781 100.3852417065

func RandNum

func RandNum(length int) (str string)

RandNum 随机生成指定长度的整数

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

4087577164

func RandStr

func RandStr(num int) string

RandStr 生成指定长度的字符串

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

62c7281i0gg5ejk54gcdmg3561793437

func Sha256

func Sha256(b []byte) string

Sha256 Sha256加密

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

7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a

func Sha256Any

func Sha256Any(str any) string

Sha256Any Sha256加密

Example
var s = struct {
	Name  string
	Phone string
}{
	"Teval", "15656565656",
}
fmt.Println(Sha256Any(s))
fmt.Println(Sha256Any(fmt.Sprintf(`%+v`, s)))
fmt.Println(fmt.Sprintf(`%+v`, s))
Output:

e0866e43fa7f387f59dac989699b73195e1b6b3d84e7e2ceb923c21c2b227e6b
e0866e43fa7f387f59dac989699b73195e1b6b3d84e7e2ceb923c21c2b227e6b
{Name:Teval Phone:15656565656}

func Sha256Str

func Sha256Str(str string) string

Sha256Str Sha256加密

Example
fmt.Println(Sha256Str("abcdefg"))
Output:

7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a

func Space

func Space(n int) string

Space 返回指定数量的空格

Example
fmt.Println(fmt.Sprintf(`_%s_`, Space(2)))
fmt.Println(len(Space(2)))
Output:

_  _
2

func Str

func Str(v any) string

Str 转为string

Example
fmt.Println(fmt.Sprintf(`%v %T`, Str(nil), Str(nil)))
fmt.Println(fmt.Sprintf(`%v %T`, Str(5), Str(5)))
Output:

string
5 string

func StrToFloat32

func StrToFloat32(str string) (f32 float32)

StrToFloat32 string转float32

Example
f32 := StrToFloat32("5.2648")
fmt.Println(fmt.Sprintf(`%v %T`, f32, f32))
Output:

5.2648 float32

func StrToFloat64

func StrToFloat64(str string) (f64 float64)

StrToFloat64 string转float64

Example
f64 := StrToFloat64("5.2648")
fmt.Println(fmt.Sprintf(`%v %T`, f64, f64))
Output:

5.2648 float64

func Trim

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

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

- 默认cutest "[\\s\\t\\r\\n]+"

Example
cutset := "[\\s\\t\\r\\n1]+"
fmt.Println(Trim(" 	\r\n	56 66  1", cutset))
fmt.Println(len(Trim(" 	\r\n	56 66  1", cutset)))
Output:

56 66
5

func UUID

func UUID() string

UUID 生成UUID

Example
fmt.Println(UUID())
Output:

ea0935aa-4cbb-4bf3-8075-bfd81569baae

func UniqID

func UniqID() string

UniqID 生成唯一字串

Example
fmt.Println(UniqID())
Output:

2633020db8d74ce93f930ee98bc27fd3

func XID

func XID() string

XID 生成唯一ID

Example
fmt.Println(XID())
Output:

c7282dogg5el2u4ikgvg

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