From 03d331dfb61f730031fc966ce2783f04deaaddc4 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 30 Aug 2023 11:46:16 +0800 Subject: [PATCH] doc: update doc for website --- README.md | 2 +- README_zh-CN.md | 2 +- docs/.vitepress/en.ts | 4 + docs/.vitepress/zh.ts | 4 + docs/api/packages/system.md | 307 ++++++++ docs/api/packages/tuple.md | 1198 +++++++++++++++++++++++++++++ docs/api/packages/validator.md | 1193 ++++++++++++++++++++++++++++ docs/api/packages/xerror.md | 491 ++++++++++++ docs/en/api/packages/system.md | 308 ++++++++ docs/en/api/packages/tuple.md | 1198 +++++++++++++++++++++++++++++ docs/en/api/packages/validator.md | 1190 ++++++++++++++++++++++++++++ docs/en/api/packages/xerror.md | 489 ++++++++++++ docs/guide/introduction.md | 2 +- 13 files changed, 6385 insertions(+), 3 deletions(-) create mode 100644 docs/api/packages/system.md create mode 100644 docs/api/packages/tuple.md create mode 100644 docs/api/packages/validator.md create mode 100644 docs/api/packages/xerror.md create mode 100644 docs/en/api/packages/system.md create mode 100644 docs/en/api/packages/tuple.md create mode 100644 docs/en/api/packages/validator.md create mode 100644 docs/en/api/packages/xerror.md diff --git a/README.md b/README.md index ec08343..105a2f8 100644 --- a/README.md +++ b/README.md @@ -1846,7 +1846,7 @@ import "github.com/duke-git/lancet/v2/xerror" [[play](https://go.dev/play/p/LKMLep723tu)] - **XError_Wrap** : creates a new XError and copy message and id to new one. [[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Wrap)] - [[play](https://go.dev/play/p/5385qT2dCi4)] + [[play](https://go.dev/play/p/RpjJ5u5sc97)] - **XError_Unwrap** : Compatible with github.com/pkg/errors. [[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror.md#XError_Unwrap)] [[play](https://go.dev/play/p/VUXJ8BST4c6)] diff --git a/README_zh-CN.md b/README_zh-CN.md index c4eac67..b560ee3 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -1853,7 +1853,7 @@ import "github.com/duke-git/lancet/v2/xerror" [[play](https://go.dev/play/p/LKMLep723tu)] - **XError_Wrap** : 创建新的 XError 对象并将消息和 id 复制到新的对象中。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Wrap)] - [[play](https://go.dev/play/p/5385qT2dCi4)] + [[play](https://go.dev/play/p/RpjJ5u5sc97)] - **XError_Unwrap** : 解构 XEerror 为 error 对象。适配 github.com/pkg/errors。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#XError_Unwrap)] [[play](https://go.dev/play/p/VUXJ8BST4c6)] diff --git a/docs/.vitepress/en.ts b/docs/.vitepress/en.ts index 2e18192..b4fdd80 100644 --- a/docs/.vitepress/en.ts +++ b/docs/.vitepress/en.ts @@ -109,6 +109,10 @@ export const enConfig: LocaleSpecificConfig = { { text: 'stream', link: '/en/api/packages/stream' }, { text: 'struct', link: '/en/api/packages/struct' }, { text: 'strutil', link: '/en/api/packages/strutil' }, + { text: 'system', link: '/en/api/packages/system' }, + { text: 'tuple', link: '/en/api/packages/tuple' }, + { text: 'validator', link: '/en/api/packages/validator' }, + { text: 'xerror', link: '/en/api/packages/xerror' }, ], }, ], diff --git a/docs/.vitepress/zh.ts b/docs/.vitepress/zh.ts index 7ddfb1e..5d5ed9a 100644 --- a/docs/.vitepress/zh.ts +++ b/docs/.vitepress/zh.ts @@ -122,6 +122,10 @@ export const zhConfig: LocaleSpecificConfig = { { text: '流', link: '/api/packages/stream' }, { text: '结构体', link: '/api/packages/struct' }, { text: '字符串', link: '/api/packages/strutil' }, + { text: '系统', link: '/api/packages/system' }, + { text: '元组', link: '/api/packages/tuple' }, + { text: '验证器', link: '/api/packages/validator' }, + { text: '错误处理', link: '/api/packages/xerror' }, ], }, ], diff --git a/docs/api/packages/system.md b/docs/api/packages/system.md new file mode 100644 index 0000000..3d5d360 --- /dev/null +++ b/docs/api/packages/system.md @@ -0,0 +1,307 @@ +# System + +system 包含 os, runtime, shell command 相关函数。 + +
+ +## 源码: + +- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go) + +
+ +## 用法: + +```go +import ( + "github.com/duke-git/lancet/v2/system" +) +``` + +
+ +## 目录 + +- [IsWindows](#IsWindows) +- [IsLinux](#IsLinux) +- [IsMac](#IsMac) +- [GetOsEnv](#GetOsEnv) +- [SetOsEnv](#SetOsEnv) +- [RemoveOsEnv](#RemoveOsEnv) +- [CompareOsEnv](#CompareOsEnv) +- [ExecCommand](#ExecCommand) +- [GetOsBits](#GetOsBits) + +
+ +## 文档 + +### IsWindows + +

检查当前操作系统是否是windows

+ +函数签名: + +```go +func IsWindows() bool +``` + +示例:[运行](https://go.dev/play/p/zIflQgZNuxD) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + isOsWindows := system.IsWindows() + fmt.Println(isOsWindows) +} +``` + +### IsLinux + +

检查当前操作系统是否是linux

+ +函数签名:[运行](https://go.dev/play/p/zIflQgZNuxD) + +```go +func IsLinux() bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + isOsLinux := system.IsLinux() + fmt.Println(isOsLinux) +} +``` + +### IsMac + +

检查当前操作系统是否是macos

+ +函数签名: + +```go +func IsMac() bool +``` + +示例:[运行](https://go.dev/play/p/Mg4Hjtyq7Zc) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + isOsMac := system.IsMac() + fmt.Println(isOsMac) +} +``` + +### GetOsEnv + +

获取key命名的环境变量的值

+ +函数签名: + +```go +func GetOsEnv(key string) string +``` + +示例:[运行](https://go.dev/play/p/D88OYVCyjO-) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + err := system.SetOsEnv("foo", "abc") + result := system.GetOsEnv("foo") + + fmt.Println(err) + fmt.Println(result) + // Output: + // + // abc +} +``` + +### SetOsEnv + +

设置由key命名的环境变量的值

+ +函数签名: + +```go +func SetOsEnv(key, value string) error +``` + +示例:[运行](https://go.dev/play/p/D88OYVCyjO-) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + err := system.SetOsEnv("foo", "abc") + result := system.GetOsEnv("foo") + + fmt.Println(err) + fmt.Println(result) + // Output: + // + // abc +} +``` + +### RemoveOsEnv + +

删除单个环境变量

+ +函数签名: + +```go +func RemoveOsEnv(key string) error +``` + +示例:[运行](https://go.dev/play/p/fqyq4b3xUFQ) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + err1 := system.SetOsEnv("foo", "abc") + result1 := GetOsEnv("foo") + + err2 := system.RemoveOsEnv("foo") + result2 := GetOsEnv("foo") + + fmt.Println(err1) + fmt.Println(err2) + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // + // + // abc + // +} +``` + +### CompareOsEnv + +

获取key命名的环境变量值并与compareEnv进行比较

+ +函数签名: + +```go +func CompareOsEnv(key, comparedEnv string) bool +``` + +示例:[运行](https://go.dev/play/p/BciHrKYOHbp) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + err := system.SetOsEnv("foo", "abc") + if err != nil { + return + } + + result := system.CompareOsEnv("foo", "abc") + + fmt.Println(result) + + // Output: + // true +} +``` + +### ExecCommand + +

执行shell命令,返回命令的stdout和stderr字符串,如果出现错误,则返回错误。参数`command`是一个完整的命令字符串,如ls-a(linux),dir(windows),ping 127.0.0.1。在linux中,使用/bin/bash-c执行命令,在windows中,使用powershell.exe执行命令。

+ +函数签名: + +```go +type ( + Option func(*exec.Cmd) +) +func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error) +``` + +示例:[运行](https://go.dev/play/p/n-2fLyZef-4) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + // linux or mac + stdout, stderr, err := system.ExecCommand("ls") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + assert.Equal("", stderr) + + // windows + stdout, stderr, err = system.ExecCommand("dir") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + + // error command + stdout, stderr, err = system.ExecCommand("abc") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + if err != nil { + fmt.Println(err.Error()) + } +} +``` + +### GetOsBits + +

获取当前操作系统位数,返回32或64

+ +函数签名: + +```go +func GetOsBits() int +``` + +示例:[运行](https://go.dev/play/p/ml-_XH3gJbW) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + osBit := system.GetOsBits() + fmt.Println(osBit) // 32 or 64 +} +``` diff --git a/docs/api/packages/tuple.md b/docs/api/packages/tuple.md new file mode 100644 index 0000000..5c50e61 --- /dev/null +++ b/docs/api/packages/tuple.md @@ -0,0 +1,1198 @@ +# Tuple + +tuple包实现一个元组数据类型。 + +
+ +## 源码: + +- [https://github.com/duke-git/lancet/blob/main/tuple/tuple.go](https://github.com/duke-git/lancet/blob/main/tuple/tuple.go) + +
+ +## 用法: + +```go +import ( + "github.com/duke-git/lancet/v2/pointer" +) +``` + +
+ +## 目录 + +- [Tuple2](#Tuple2) +- [Tuple2_Unbox](#Tuple2_Unbox) +- [Zip2](#Zip2) +- [Unzip2](#Unzip2) +- [Tuple3](#Tuple3) +- [Tuple3_Unbox](#Tuple3_Unbox) +- [Zip3](#Zip3) +- [Unzip3](#Unzip3) +- [Tuple4](#Tuple4) +- [Tuple4_Unbox](#Tuple4_Unbox) +- [Zip4](#Zip4) +- [Unzip4](#Unzip4) +- [Tuple5](#Tuple5) +- [Tuple5_Unbox](#Tuple5_Unbox) +- [Zip5](#Zip5) +- [Unzip5](#Unzip5) +- [Tuple6](#Tuple6) +- [Tuple6_Unbox](#Tuple6_Unbox) +- [Zip6](#Zip6) +- [Unzip6](#Unzip6) +- [Tuple7](#Tuple7) +- [Tuple7_Unbox](#Tuple7_Unbox) +- [Zip7](#Zip7) +- [Unzip7](#Unzip7) +- [Tuple8](#TTuple8uple6) +- [Tuple8_Unbox](#Tuple8_Unbox) +- [Zip8](#Zip8) +- [Unzip8](#Unzip8) +- [Tuple9](#Tuple9) +- [Tuple9_Unbox](#Tuple9_Unbox) +- [Zip9](#Zip9) +- [Unzip9](#Unzip9) +- [Tuple10](#Tuple10) +- [Tuple10_Unbox](#Tuple10_Unbox) +- [Zip10](#Zip10) +- [Unzip10](#Unzip10) + +
+ + +## 文档 + +### Tuple2 + +

2元元组

+ +函数签名: + +```go +type Tuple2[A any, B any] struct { + FieldA A + FieldB B +} + +func NewTuple2[A any, B any](a A, b B) Tuple2[A, B] +``` + +示例:[运行](https://go.dev/play/p/3sHVqBQpLYN) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple2(1, 0.1) + fmt.Printf("%v %v", t.FieldA, t.FieldB) + + // Output: 1 0.1 +} +``` + +### Tuple2_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple2[A, B]) Unbox() (A, B) +``` + +示例:[运行](https://go.dev/play/p/0fD1qfCVwjm) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple2(1, 0.1) + v1, v2 := t.Unbox() + fmt.Printf("%v %v", v1, v2) + + // Output: 1 0.1 +} +``` + +### Zip2 + +

创建一个Tuple2元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip2[A any, B any](a []A, b []B) []Tuple2[A, B] +``` + +示例:[运行](https://go.dev/play/p/4ncWJJ77Xio) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip2([]int{1}, []float64{0.1}) + fmt.Println(result) + + // Output: [{1 0.1}] +} +``` + +### Unzip2 + +

根据传入的Tuple2切片,创建一组和Tuple2元素相对应的切片。

+ +函数签名: + +```go +func Unzip2[A any, B any](tuples []Tuple2[A, B]) ([]A, []B) +``` + +示例:[运行](https://go.dev/play/p/KBecr60feXb) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2 := tuple.Unzip2([]tuple.Tuple2[int, float64]{{FieldA: 1, FieldB: 0.1}}) + + fmt.Printf("%v %v", v1, v2) + + // Output: [1] [0.1] +} +``` + +### Tuple3 + +

3元元组。

+ +函数签名: + +```go +type Tuple3[A any, B any, C any] struct { + FieldA A + FieldB B + FieldC C +} + +func NewTuple3[A any, B any, C any](a A, b B c C) Tuple3[A, B, C] +``` + +示例:[运行](https://go.dev/play/p/FtH2sdCLlCf) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple3(1, 0.1, "a") + fmt.Printf("%v %v %v", t.FieldA, t.FieldB, t.FieldC) + + // Output: 1 0.1 a +} +``` + +### Tuple3_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple3[A, B, C]) Unbox() (A, B, C) +``` + +示例:[运行](https://go.dev/play/p/YojLy-id1BS) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple2(1, 0.1, "a") + v1, v2, v3 := t.Unbox() + fmt.Printf("%v %v %v", v1, v2, v3) + + // Output: 1 0.1 a +} +``` + +### Zip3 + +

创建一个Tuple3元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip3[A any, B any, C any](a []A, b []B, c []C) []Tuple3[A, B, C] +``` + +示例:[运行](https://go.dev/play/p/97NgmsTILfu) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip3([]int{1}, []float64{0.1}, []string{"a"}) + fmt.Println(result) + + // Output: [{1 0.1 a}] +} +``` + +### Unzip3 + +

根据传入的Tuple3切片,创建一组和Tuple3元素相对应的切片。

+ +函数签名: + +```go +func Unzip3[A any, B any, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C) +``` + +示例:[运行](https://go.dev/play/p/bba4cpAa7KO) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3 := tuple.Unzip3([]tuple.Tuple3[int, float64, string]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a"}, + }) + + fmt.Printf("%v %v %v", v1, v2, v3) + + // Output: [1] [0.1] [a] +} +``` + +### Tuple4 + +

4元元组。

+ +函数签名: + +```go +type Tuple4[A any, B any, C any, D any] struct { + FieldA A + FieldB B + FieldC C + FieldD D +} + +func NewTuple4[A any, B any, C any, D any](a A, b B, c C, d D) Tuple4[A, B, C, D] +``` + +示例:[运行](https://go.dev/play/p/D2EqDz096tk) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple4(1, 0.1, "a", true) + fmt.Printf("%v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD) + + // Output: 1 0.1 a true +} +``` + +### Tuple4_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple4[A, B, C, D]) Unbox() (A, B, C, D) +``` + +示例:[运行](https://go.dev/play/p/ACj9YuACGgW) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple4(1, 0.1, "a", true) + v1, v2, v3, v4 := t.Unbox() + fmt.Printf("%v %v %v %v", v1, v2, v3, v4) + + // Output: 1 0.1 a true +} +``` + +### Zip4 + +

创建一个Tuple4元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip4[A any, B any, C any, D any](a []A, b []B, c []C, d []D) []Tuple4[A, B, C, D] +``` + +示例:[运行](https://go.dev/play/p/PEmTYVK5hL4) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip4([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}) + fmt.Println(result) + + // Output: [{1 0.1 a true}] +} +``` + +### Unzip4 + +

根据传入的Tuple4切片,创建一组和Tuple4元素相对应的切片。

+ +函数签名: + +```go +func Unzip4[A any, B any, C any, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D) +``` + +示例:[运行](https://go.dev/play/p/rb8z4gyYSRN) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4 := tuple.Unzip4([]tuple.Tuple4[int, float64, string, bool]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true}, + }) + + fmt.Printf("%v %v %v %v", v1, v2, v3, v4) + + // Output: [1] [0.1] [a] [true] +} +``` + +### Tuple5 + +

5元元组。

+ +函数签名: + +```go +type Tuple5[A any, B any, C any, D any, E any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E +} + +func NewTuple5[A any, B any, C any, D any, E any](a A, b B, c C, d D, e E) Tuple5[A, B, C, D, E] +``` + +示例:[运行](https://go.dev/play/p/2WndmVxPg-r) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple5(1, 0.1, "a", true, 2) + fmt.Printf("%v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE) + + // Output: 1 0.1 a true 2 +} +``` + +### Tuple5_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple5[A, B, C, D, E]) Unbox() (A, B, C, D, E) +``` + +示例:[运行](https://go.dev/play/p/GyIyZHjCvoS) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple5(1, 0.1, "a", true, 2) + v1, v2, v3, v4, v5 := t.Unbox() + fmt.Printf("%v %v %v %v %v", v1, v2, v3, v4, v5) + + // Output: 1 0.1 a true 2 +} +``` + +### Zip5 + +

创建一个Tuple5元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip5[A any, B any, C any, D any, E any](a []A, b []B, c []C, d []D, e []E) []Tuple5[A, B, C, D, E] +``` + +示例:[运行](https://go.dev/play/p/fCAAJLMfBIP) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip5([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2}] +} +``` + +### Unzip5 + +

根据传入的Tuple5切片,创建一组和Tuple5元素相对应的切片。

+ +函数签名: + +```go +func Unzip5[A any, B any, C any, D any, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, []D, []E) +``` + +示例:[运行](https://go.dev/play/p/gyl6vKfhqPb) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5 := tuple.Unzip5([]tuple.Tuple5[int, float64, string, bool, int]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2}, + }) + + fmt.Printf("%v %v %v %v %v", v1, v2, v3, v4, v5) + + // Output: [1] [0.1] [a] [true] [2] +} +``` + +### Tuple6 + +

6元元组。

+ +函数签名: + +```go +type Tuple6[A any, B any, C any, D any, E any, F any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F +} + +func NewTuple6[A any, B any, C any, D any, E any, F any](a A, b B, c C, d D, e E, f F) Tuple6[A, B, C, D, E, F] +``` + +示例:[运行](https://go.dev/play/p/VjqcCwEJZbs) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple6(1, 0.1, "a", true, 2, 2.2) + fmt.Printf("%v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF) + + // Output: 1 0.1 a true 2 2.2 +} +``` + +### Tuple6_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple6[A, B, C, D, E, F]) Unbox() (A, B, C, D, E, F) +``` + +示例:[运行](https://go.dev/play/p/FjIHV7lpxmW) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple6(1, 0.1, "a", true, 2, 2.2) + v1, v2, v3, v4, v5, v6 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v", v1, v2, v3, v4, v5, v6) + + // Output: 1 0.1 a true 2 2.2 +} +``` + +### Zip6 + +

创建一个Tuple6元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip6[A any, B any, C any, D any, E any, F any](a []A, b []B, c []C, d []D, e []E, f []F) []Tuple6[A, B, C, D, E, F] +``` + +示例:[运行](https://go.dev/play/p/oWPrnUYuFHo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip6([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2}] +} +``` + +### Unzip6 + +

根据传入的Tuple6切片,创建一组和Tuple6元素相对应的切片。

+ +函数签名: + +```go +func Unzip6[A any, B any, C any, D any, E any, F any](tuples []Tuple6[A, B, C, D, E, F]) ([]A, []B, []C, []D, []E, []F) +``` + +示例:[运行](https://go.dev/play/p/l41XFqCyh5E) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6 := tuple.Unzip6([]tuple.Tuple6[int, float64, string, bool, int, float32]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2}, + }) + + fmt.Printf("%v %v %v %v %v %v", v1, v2, v3, v4, v5, v6) + + // Output: [1] [0.1] [a] [true] [2] [2.2] +} +``` + +### Tuple7 + +

7元元组。

+ +函数签名: + +```go +type Tuple7[A any, B any, C any, D any, E any, F any, G any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F + FieldG G +} + +func NewTuple7[A any, B any, C any, D any, E any, F any, G any](a A, b B, c C, d D, e E, f F, g G) Tuple7[A, B, C, D, E, F, G] +``` + +示例:[运行](https://go.dev/play/p/dzAgv_Ezub9) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple7(1, 0.1, "a", true, 2, 2.2, "b") + fmt.Printf("%v %v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF, t.FieldG) + + // Output: 1 0.1 a true 2 2.2 b +} +``` + +### Tuple7_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple7[A, B, C, D, E, F, G]) Unbox() (A, B, C, D, E, F, G) +``` + +示例:[运行](https://go.dev/play/p/R9I8qeDk0zs) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple7(1, 0.1, "a", true, 2, 2.2, "b") + v1, v2, v3, v4, v5, v6, v7 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7) + + // Output: 1 0.1 a true 2 2.2 b +} +``` + +### Zip7 + +

创建一个Tuple7元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip7[A any, B any, C any, D any, E any, F any, G any](a []A, b []B, c []C, d []D, e []E, f []F, g []G) []Tuple7[A, B, C, D, E, F, G] +``` + +示例:[运行](https://go.dev/play/p/WUJuo897Egf) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip7([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}, []string{"b"}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2 b}] +} +``` + +### Unzip7 + +

根据传入的Tuple7切片,创建一组和Tuple7元素相对应的切片。

+ +函数签名: + +```go +func Unzip7[A any, B any, C any, D any, E any, F any, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, []B, []C, []D, []E, []F, []G) +``` + +示例:[运行](https://go.dev/play/p/hws_P1Fr2j3) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6, v7 := tuple.Unzip7([]tuple.Tuple7[int, float64, string, bool, int, float32, string]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2, FieldG: "b"}, + }) + + fmt.Printf("%v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7) + + // Output: [1] [0.1] [a] [true] [2] [2.2] [b] +} +``` + +### Tuple8 + +

8元元组。

+ +函数签名: + +```go +type Tuple8[A any, B any, C any, D any, E any, F any, G any, H any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F + FieldG G + FieldH H +} + +func NewTuple8[A any, B any, C any, D any, E any, F any, G any, H any](a A, b B, c C, d D, e E, f F, g G, h H) Tuple8[A, B, C, D, E, F, G, H] +``` + +示例:[运行](https://go.dev/play/p/YA9S0rz3dRz) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple8(1, 0.1, "a", true, 2, 2.2, "b", "c") + fmt.Printf("%v %v %v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF, t.FieldG, t.FieldH) + + // Output: 1 0.1 a true 2 2.2 b c +} +``` + +### Tuple8_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple8[A, B, C, D, E, F, G, H]) Unbox() (A, B, C, D, E, F, G, H) +``` + +示例:[运行](https://go.dev/play/p/PRxLBBb4SMl) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple8(1, 0.1, "a", true, 2, 2.2, "b", "c") + v1, v2, v3, v4, v5, v6, v7, v8 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8) + + // Output: 1 0.1 a true 2 2.2 b c +} +``` + +### Zip8 + +

创建一个Tuple8元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip8[A any, B any, C any, D any, E any, F any, G any, H any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H) []Tuple8[A, B, C, D, E, F, G, H] +``` + +示例:[运行](https://go.dev/play/p/8V9jWkuJfaQ) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip8([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}, []string{"b"}, []string{"c"}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2 b c}] +} +``` + +### Unzip8 + +

根据传入的Tuple8切片,创建一组和Tuple8元素相对应的切片。

+ +函数签名: + +```go +func Unzip8[A any, B any, C any, D any, E any, F any, G any, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) ([]A, []B, []C, []D, []E, []F, []G, []H) +``` + +示例:[运行](https://go.dev/play/p/1SndOwGsZB4) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6, v7, v8 := tuple.Unzip8([]tuple.Tuple8[int, float64, string, bool, int, float32, string, string]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2, FieldG: "b", FieldH: "c"}, + }) + + fmt.Printf("%v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8) + + // Output: [1] [0.1] [a] [true] [2] [2.2] [b] [c] +} +``` + +### Tuple9 + +

9元元组。

+ +函数签名: + +```go + +type Tuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F + FieldG G + FieldH H + FieldI I +} + +func NewTuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) Tuple9[A, B, C, D, E, F, G, H, I] + +``` + +示例:[运行](https://go.dev/play/p/yS2NGGtZpQr) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple9(1, 0.1, "a", true, 2, 2.2, "b", "c", map[string]int{"a": 1}) + fmt.Printf("%v %v %v %v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF, t.FieldG, t.FieldH, t.FieldI) + + // Output: 1 0.1 a true 2 2.2 b c map[a:1] +} +``` + +### Tuple9_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple9[A, B, C, D, E, F, G, H, I]) Unbox() (A, B, C, D, E, F, G, H, I) +``` + +示例:[运行](https://go.dev/play/p/oFJFGTAuOa8) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple9(1, 0.1, "a", true, 2, 2.2, "b", "c", map[string]int{"a": 1}) + v1, v2, v3, v4, v5, v6, v7, v8, v9 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8, v9) + + // Output: 1 0.1 a true 2 2.2 b c map[a:1] +} +``` + +### Zip9 + +

创建一个Tuple9元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip9[A any, B any, C any, D any, E any, F any, G any, H any, I any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I) []Tuple9[A, B, C, D, E, F, G, H, I] +``` + +示例:[运行](https://go.dev/play/p/cgsL15QYnfz) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip9([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}, []string{"b"}, []string{"c"}, []int64{3}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2 b c 3}] +} +``` + +### Unzip9 + +

根据传入的Tuple9切片,创建一组和Tuple9元素相对应的切片。

+ +函数签名: + +```go +func Unzip9[A any, B any, C any, D any, E any, F any, G any, H any, I any](tuples []Tuple9[A, B, C, D, E, F, G, H, I]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I) +``` + +示例:[运行](https://go.dev/play/p/91-BU_KURSA) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6, v7, v8, v9 := tuple.Unzip9([]tuple.Tuple9[int, float64, string, bool, int, float32, string, string, int64]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2, FieldG: "b", FieldH: "c", FieldI: 3}, + }) + + fmt.Printf("%v %v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8, v9) + + // Output: [1] [0.1] [a] [true] [2] [2.2] [b] [c] [3] +} +``` + +### Tuple10 + +

10元元组。

+ +函数签名: + +```go + +type Tuple10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F + FieldG G + FieldH H + FieldI I + FieldJ J +} + +func NewTuple10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any](a A, b B, c C, d D, e E, f F, g G, h H, i I, j J) Tuple10[A, B, C, D, E, F, G, H, I, J] + +``` + +示例:[运行](https://go.dev/play/p/799qqZg0hUv) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + type foo struct { + A string + } + t := tuple.NewTuple10(1, 0.1, "a", true, 2, 2.2, "b", "c", map[string]int{"a": 1}, foo{A: "a"}) + fmt.Printf("%v %v %v %v %v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF, t.FieldG, t.FieldH, t.FieldI, t.FieldJ) + + // Output: 1 0.1 a true 2 2.2 b c map[a:1] {a} +} +``` + +### Tuple10_Unbox + +

返回元组的字段值。

+ +函数签名: + +```go +func (t Tuple10[A, B, C, D, E, F, G, H, I, J]) Unbox() (A, B, C, D, E, F, G, H, I, J) +``` + +示例:[运行](https://go.dev/play/p/qfyx3x_X0Cu) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + type foo struct { + A string + } + t := tuple.NewTuple10(1, 0.1, "a", true, 2, 2.2, "b", "c", map[string]int{"a": 1}, foo{A: "a"}) + v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) + + // Output: 1 0.1 a true 2 2.2 b c map[a:1] {a} +} +``` + +### Zip10 + +

创建一个Tuple10元组切片, 其中元组的元素和传入切片元素相对应。

+ +函数签名: + +```go +func Zip10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I, j []J) []Tuple10[A, B, C, D, E, F, G, H, I, J] +``` + +示例:[运行](https://go.dev/play/p/YSR-2cXnrY4) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip10([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}, []string{"b"}, []string{"c"}, []int64{3}, []bool{false}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2 b c 3 false}] +} +``` + +### Unzip10 + +

根据传入的Tuple10切片,创建一组和Tuple10元素相对应的切片。

+ +函数签名: + +```go +func Unzip10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any](tuples []Tuple10[A, B, C, D, E, F, G, H, I, J]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I, []J) +``` + +示例:[运行](https://go.dev/play/p/-taQB6Wfre_z) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 := tuple.Unzip10([]tuple.Tuple10[int, float64, string, bool, int, float32, string, string, int64, bool]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2, FieldG: "b", FieldH: "c", FieldI: 3, FieldJ: false}, + }) + + fmt.Printf("%v %v %v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) + + // Output: [1] [0.1] [a] [true] [2] [2.2] [b] [c] [3] [false] +} +``` diff --git a/docs/api/packages/validator.md b/docs/api/packages/validator.md new file mode 100644 index 0000000..daf4ca5 --- /dev/null +++ b/docs/api/packages/validator.md @@ -0,0 +1,1193 @@ +# Validator + +validator 验证器包,包含常用字符串格式验证函数。 + +
+ +## 源码: + +- [https://github.com/duke-git/lancet/blob/main/validator/validator.go](https://github.com/duke-git/lancet/blob/main/validator/validator.go) + +
+ +## 用法: + +```go +import ( + "github.com/duke-git/lancet/v2/validator" +) +``` + +
+ +## 目录: + +- [ContainChinese](#ContainChinese) +- [ContainLetter](#ContainLetter) +- [ContainLower](#ContainLower) +- [ContainUpper](#ContainUpper) +- [IsAlpha](#IsAlpha) +- [IsAllUpper](#IsAllUpper) +- [IsAllLower](#IsAllLower) +- [IsASCII](#IsASCII) +- [IsBase64](#IsBase64) +- [IsChineseMobile](#IsChineseMobile) +- [IsChineseIdNum](#IsChineseIdNum) +- [IsChinesePhone](#IsChinesePhone) +- [IsCreditCard](#IsCreditCard) +- [IsDns](#IsDns) +- [IsEmail](#IsEmail) +- [IsEmptyString](#IsEmptyString) +- [IsInt](#IsInt) +- [IsFloat](#IsFloat) +- [IsNumber](#IsNumber) +- [IsIntStr](#IsIntStr) +- [IsFloatStr](#IsFloatStr) +- [IsNumberStr](#IsNumberStr) +- [IsJSON](#IsJSON) +- [IsRegexMatch](#IsRegexMatch) +- [IsIp](#IsIp) +- [IsIpV4](#IsIpV4) +- [IsIpV6](#IsIpV6) +- [IsStrongPassword](#IsStrongPassword) +- [IsUrl](#IsUrl) +- [IsWeakPassword](#IsWeakPassword) +- [IsZeroValue](#IsZeroValue) +- [IsGBK](#IsGBK) +- [IsPrintable](#IsPrintable) + +
+ + +## 文档 + +### ContainChinese + +

验证字符串是否包含中文字符。

+ +函数签名: + +```go +func ContainChinese(s string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.ContainChinese("你好") + result2 := validator.ContainChinese("你好hello") + result3 := validator.ContainChinese("hello") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### ContainLetter + +

验证字符串是否包含至少一个英文字母。

+ +函数签名: + +```go +func ContainLetter(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.ContainLetter("你好") + result2 := validator.ContainLetter("&@#$%^&*") + result3 := validator.ContainLetter("ab1") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // false + // false + // true +} +``` + +### ContainLower + +

验证字符串是否包含至少一个英文小写字母。

+ +函数签名: + +```go +func ContainLower(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.ContainLower("abc") + result2 := validator.ContainLower("aBC") + result3 := validator.ContainLower("ABC") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### ContainUpper + +

验证字符串是否包含至少一个英文大写字母。

+ +函数签名: + +```go +func ContainUpper(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.ContainUpper("ABC") + result2 := validator.ContainUpper("abC") + result3 := validator.ContainUpper("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### IsAlpha + +

验证字符串是否只包含英文字母。

+ +函数签名: + +```go +func IsAlpha(s string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsAlpha("abc") + result2 := validator.IsAlpha("ab1") + result3 := validator.IsAlpha("") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsAllUpper + +

验证字符串是否全是大写英文字母。

+ +函数签名: + +```go +func IsAllUpper(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsAllUpper("ABC") + result2 := validator.IsAllUpper("ABc") + result3 := validator.IsAllUpper("AB1") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsAllLower + +

验证字符串是否全是小写英文字母。

+ +函数签名: + +```go +func IsAllLower(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsAllLower("abc") + result2 := validator.IsAllLower("abC") + result3 := validator.IsAllLower("ab1") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsASCII + +

验证字符串全部为ASCII字符。

+ +函数签名: + +```go +func IsASCII(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsASCII("ABC") + result2 := validator.IsASCII("123") + result3 := validator.IsASCII("") + result4 := validator.IsASCII("😄") + result5 := validator.IsASCII("你好") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // true + // true + // true + // false + // false +} +``` + +### IsBase64 + +

验证字符串是否是base64编码。

+ +函数签名: + +```go +func IsBase64(base64 string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsBase64("aGVsbG8=") + result2 := validator.IsBase64("123456") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsChineseMobile + +

验证字符串是否是中国手机号码。

+ +函数签名: + +```go +func IsChineseMobile(mobileNum string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsChineseMobile("13263527980") + result2 := validator.IsChineseMobile("434324324") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsChineseIdNum + +

验证字符串是否是中国身份证号码。

+ +函数签名: + +```go +func IsChineseIdNum(id string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsChineseIdNum("210911192105130715") + result2 := validator.IsChineseIdNum("123456") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsChinesePhone + +

验证字符串是否是中国电话座机号码。

+ +函数签名: + +```go +func IsChinesePhone(phone string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsChinesePhone("010-32116675") + result2 := validator.IsChinesePhone("123-87562") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsCreditCard + +

验证字符串是否是信用卡号码。

+ +函数签名: + +```go +func IsCreditCard(creditCart string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsCreditCard("4111111111111111") + result2 := validator.IsCreditCard("123456") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsDns + +

验证字符串是否是有效dns。

+ +函数签名: + +```go +func IsDns(dns string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsDns("abc.com") + result2 := validator.IsDns("a.b.com") + result3 := validator.IsDns("http://abc.com") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsEmail + +

验证字符串是否是有效电子邮件地址。

+ +函数签名: + +```go +func IsEmail(email string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsEmail("abc@xyz.com") + result2 := validator.IsEmail("a.b@@com") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsEmptyString + +

验证字符串是否是空字符串。

+ +函数签名: + +```go +func IsEmptyString(s string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsEmptyString("") + result2 := validator.IsEmptyString(" ") + result3 := validator.IsEmptyString("\t") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsInt + +

验证参数是否是整数(int, unit)。

+ +函数签名: + +```go +func IsInt(v any) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsInt("") + result2 := validator.IsInt("3") + result3 := validator.IsInt(0.1) + result4 := validator.IsInt(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} +``` + +### IsFloat + +

验证参数是否是浮点数(float32, float34)。

+ +函数签名: + +```go +func IsFloat(v any) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsFloat("") + result2 := validator.IsFloat("3") + result3 := validator.IsFloat(0) + result4 := validator.IsFloat(0.1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} +``` + +### IsNumber + +

验证参数是否是数字(integer or float)

+ +函数签名: + +```go +func IsNumber(v any) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsNumber("") + result2 := validator.IsNumber("3") + result3 := validator.IsNumber(0.1) + result4 := validator.IsNumber(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // true + // true +} +``` + + +### IsIntStr + +

验证字符串是否是可以转换为整数

+ +函数签名: + +```go +func IsIntStr(s string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIntStr("+3") + result2 := validator.IsIntStr("-3") + result3 := validator.IsIntStr("3.") + result4 := validator.IsIntStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsFloatStr + +

验证字符串是否是可以转换为浮点数

+ +函数签名: + +```go +func IsFloatStr(s string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsFloatStr("3.") + result2 := validator.IsFloatStr("+3.") + result3 := validator.IsFloatStr("12") + result4 := validator.IsFloatStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false +} +``` + +### IsNumberStr + +

验证字符串是否是可以转换为数字

+ +函数签名: + +```go +func IsNumberStr(s string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsNumberStr("3.") + result2 := validator.IsNumberStr("+3.") + result3 := validator.IsNumberStr("+3e2") + result4 := validator.IsNumberStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false +} +``` + +### IsJSON + +

验证字符串是否是有效json

+ +函数签名: + +```go +func IsJSON(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsJSON("{}") + result2 := validator.IsJSON("{\"name\": \"test\"}") + result3 := validator.IsJSON("") + result4 := validator.IsJSON("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsRegexMatch + +

验证字符串是否可以匹配正则表达式

+ +函数签名: + +```go +func IsRegexMatch(s, regex string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsRegexMatch("abc", `^[a-zA-Z]+$`) + result2 := validator.IsRegexMatch("ab1", `^[a-zA-Z]+$`) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + + + +### IsIp + +

验证字符串是否是ip地址

+ +函数签名: + +```go +func IsIp(ipstr string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIp("127.0.0.1") + result2 := validator.IsIp("::0:0:0:0:0:0:1") + result3 := validator.IsIp("127.0.0") + result4 := validator.IsIp("::0:0:0:0:") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsIpV4 + +

验证字符串是否是ipv4地址

+ +函数签名: + +```go +func IsIpV4(ipstr string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIpV4("127.0.0.1") + result2 := validator.IsIpV4("::0:0:0:0:0:0:1") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsIpV6 + +

验证字符串是否是ipv6地址

+ +函数签名: + +```go +func IsIpV6(ipstr string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIpV6("127.0.0.1") + result2 := validator.IsIpV6("::0:0:0:0:0:0:1") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true +} +``` + +### IsStrongPassword + +

验证字符串是否是强密码:(alpha(lower+upper) + number + special chars(!@#$%^&*()?><))

+ +函数签名: + +```go +func IsStrongPassword(password string, length int) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsStrongPassword("abcABC", 6) + result2 := validator.IsStrongPassword("abcABC123@#$", 10) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true +} +``` + +### IsUrl + +

验证字符串是否是url

+ +函数签名: + +```go +func IsUrl(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsUrl("abc.com") + result2 := validator.IsUrl("http://abc.com") + result3 := validator.IsUrl("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### IsWeakPassword + +

验证字符串是否是弱密码:(only letter or only number or letter + number) +.

+ +函数签名: + +```go +func IsWeakPassword(password string, length int) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsWeakPassword("abcABC") + result2 := validator.IsWeakPassword("abc123@#$") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsZeroValue + +

判断传入的参数值是否为零值。

+ +函数签名: + +```go +func IsZeroValue(value any) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsZeroValue("") + result2 := validator.IsZeroValue(0) + result3 := validator.IsZeroValue("abc") + result4 := validator.IsZeroValue(1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsGBK + +

检查数据编码是否为gbk(汉字内部代码扩展规范)。该函数的实现取决于双字节是否在gbk的编码范围内,而utf-8编码格式的每个字节都在gbk编码范围内。因此,应该首先调用utf8.valid检查它是否是utf-8编码,然后调用IsGBK检查gbk编码。如示例所示。

+ +函数签名: + +```go +func IsGBK(data []byte) bool +``` + +示例: + +```go +import ( + "fmt" + "golang.org/x/text/encoding/simplifiedchinese" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + str := "你好" + gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str)) + + result := validator.IsGBK(gbkData) + + fmt.Println(result) + + // Output: + // true +} +``` + + +### IsPrintable + +

检查字符串是否全部为可打印字符。

+ +函数签名: + +```go +func IsPrintable(str string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsPrintable("ABC") + result2 := validator.IsPrintable("{id: 123}") + result3 := validator.IsPrintable("") + result4 := validator.IsPrintable("😄") + result5 := validator.IsPrintable("\u0000") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // true + // true + // true + // true + // false +} +``` \ No newline at end of file diff --git a/docs/api/packages/xerror.md b/docs/api/packages/xerror.md new file mode 100644 index 0000000..d363e11 --- /dev/null +++ b/docs/api/packages/xerror.md @@ -0,0 +1,491 @@ +# Xerror + +xerror 错误处理逻辑封装 + +
+ +## 源码: + +- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go) + +
+ +## 用法: + +```go +import ( + "github.com/duke-git/lancet/v2/xerror" +) +``` + +
+ +## 目录 + +- [New](#New) +- [Wrap](#Wrap) +- [Unwrap](#Unwrap) +- [XError_Wrap](#XError_Wrap) +- [XError_Unwrap](#XError_Unwrap) +- [XError_With](#XError_With) +- [XError_Is](#XError_Is) +- [XError_Id](#XError_Id) +- [XError_Values](#XError_Values) +- [XError_StackTrace](#XError_StackTrace) +- [XError_Info](#XError_Info) +- [XError_Error](#XError_Error) +- [TryUnwrap](#TryUnwrap) + +
+ + +## 文档 + +### New + +

创建XError对象实例。

+ +函数签名: + +```go +type XError struct { + id string + message string + stack *stack + cause error + values map[string]any +} + +func New(format string, args ...any) *XError +``` + +示例:[运行](https://go.dev/play/p/w4oWZts7q7f) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error") + fmt.Println(err.Error()) + + // Output: + // error +} +``` + +### Wrap + +

根据error对象创建XError对象实例,可添加message。

+ +函数签名: + +```go +func Wrap(cause error, message ...any) *XError +``` + +示例:[运行](https://go.dev/play/p/5385qT2dCi4) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("wrong password") + wrapErr := xerror.Wrap(err, "error") + + fmt.Println(wrapErr.Error()) + + // Output: + // error: wrong password +} +``` + +### Unwrap + +

从error对象中解构出XError。

+ +函数签名: + +```go +func Unwrap(err error) *XError +``` + +示例:[运行](https://go.dev/play/p/LKMLep723tu) + +```go +package main + +import ( + "fmt" + "github.com/pkg/errors" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").With("level", "high") + wrapErr := errors.Wrap(err1, "oops") + + err := xerror.Unwrap(wrapErr) + + values := err.Values() + fmt.Println(values["level"]) + + // Output: + // high +} +``` + +### XError_Wrap + +

创建新的XError对象并将消息和id复制到新的对象中。

+ +函数签名: + +```go +func (e *XError) Wrap(cause error) *XError +``` + +示例:[运行](https://go.dev/play/p/RpjJ5u5sc97) + +```go +package main + +import ( + "fmt" + "errors" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").With("level", "high") + err2 := err1.Wrap(errors.New("invalid username")) + + fmt.Println(err2.Error()) + + // Output: + // error: invalid username +} +``` + +### XError_Unwrap + +

解构XEerror为error对象。适配github.com/pkg/errors。

+ +函数签名: + +```go +func (e *XError) Unwrap() error +``` + +示例:[运行](https://go.dev/play/p/VUXJ8BST4c6) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").With("level", "high") + err2 := err1.Wrap(errors.New("invalid username")) + + err := err2.Unwrap() + + fmt.Println(err.Error()) + + // Output: + // invalid username +} +``` + +### XError_With + +

添加与XError对象的键和值。

+ +函数签名: + +```go +func (e *XError) With(key string, value any) *XError +``` + +示例:[运行](https://go.dev/play/p/ow8UISXX_Dp) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error").With("level", "high") + + errLevel := err.Values()["level"] + + fmt.Println(errLevel) + + // Output: + // high +} +``` + +### XError_Id + +

设置XError对象的id。

+ +函数签名: + +```go +func (e *XError) Id(id string) *XError +``` + +示例:[运行](https://go.dev/play/p/X6HBlsy58U9) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").Id("e001") + err2 := xerror.New("error").Id("e001") + err3 := xerror.New("error").Id("e003") + + equal := err1.Is(err2) + notEqual := err1.Is(err3) + + fmt.Println(equal) + fmt.Println(notEqual) + + // Output: + // true + // false +} +``` + +### XError_Is + +

检查目标error是否为XError,两个错误中的error.id是否匹配。

+ +函数签名: + +```go +func (e *XError) Is(target error) bool +``` + +示例:[运行](https://go.dev/play/p/X6HBlsy58U9) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").Id("e001") + err2 := xerror.New("error").Id("e001") + err3 := xerror.New("error").Id("e003") + + equal := err1.Is(err2) + notEqual := err1.Is(err3) + + fmt.Println(equal) + fmt.Println(notEqual) + + // Output: + // true + // false +} +``` + +### XError_Values + +

返回由With设置的键和值的映射。将合并所有XError键和值。

+ +函数签名: + +```go +func (e *XError) Values() map[string]any +``` + +示例:[运行](https://go.dev/play/p/ow8UISXX_Dp) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error").With("level", "high") + + errLevel := err.Values()["level"] + + fmt.Println(errLevel) + + // Output: + // high +} +``` + + +### XError_StackTrace + +

返回与pkg/error兼容的堆栈信息。

+ +函数签名: + +```go +func (e *XError) StackTrace() StackTrace +``` + +示例:[运行](https://go.dev/play/p/6FAvSQpa7pc) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error") + + stacks := err.Stacks() + + fmt.Println(stacks[0].Func) + fmt.Println(stacks[0].Line) + + containFile := strings.Contains(stacks[0].File, "xxx.go") + fmt.Println(containFile) +} +``` + + +### XError_Info + +

返回可打印的XError对象信息。

+ +函数签名: + +```go +func (e *XError) Info() *errInfo +``` + +示例:[运行](https://go.dev/play/p/1ZX0ME1F-Jb) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + cause := errors.New("error") + err := xerror.Wrap(cause, "invalid username").Id("e001").With("level", "high") + + errInfo := err.Info() + + fmt.Println(errInfo.Id) + fmt.Println(errInfo.Cause) + fmt.Println(errInfo.Values["level"]) + fmt.Println(errInfo.Message) + + // Output: + // e001 + // error + // high + // invalid username +} +``` + + +### XError_Error + +

实现标准库的error接口。

+ +函数签名: + +```go +func (e *XError) Error() string +``` + +示例:[运行](https://go.dev/play/p/w4oWZts7q7f) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error") + fmt.Println(err.Error()) + + // Output: + // error +} +``` + +### TryUnwrap + +

检查error, 如果err为nil则展开,则它返回一个有效值,如果err不是nil则TryUnwrap使用err发生panic。

+ +函数签名: + +```go +func TryUnwrap[T any](val T, err error) T +``` + +示例:[运行](https://go.dev/play/p/acyZVkNZEeW) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + result1 := xerror.TryUnwrap(strconv.Atoi("42")) + fmt.Println(result1) + + _, err := strconv.Atoi("4o2") + defer func() { + v := recover() + result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error()) + fmt.Println(result2) + }() + + xerror.TryUnwrap(strconv.Atoi("4o2")) + + // Output: + // 42 + // true +} +``` diff --git a/docs/en/api/packages/system.md b/docs/en/api/packages/system.md new file mode 100644 index 0000000..5f0747e --- /dev/null +++ b/docs/en/api/packages/system.md @@ -0,0 +1,308 @@ +# System + +Package system contains some functions about os, runtime, shell command. + +
+ +## Source: + +- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go) + +
+ +## Usage: + +```go +import ( + "github.com/duke-git/lancet/v2/system" +) +``` + +
+ +## Index + +- [IsWindows](#IsWindows) +- [IsLinux](#IsLinux) +- [IsMac](#IsMac) +- [GetOsEnv](#GetOsEnv) +- [SetOsEnv](#SetOsEnv) +- [RemoveOsEnv](#RemoveOsEnv) +- [CompareOsEnv](#CompareOsEnv) +- [ExecCommand](#ExecCommand) +- [GetOsBits](#GetOsBits) + +
+ + +## Documentation + +### IsWindows + +

Check if current os is windows.

+ +Signature: + +```go +func IsWindows() bool +``` + +Example:[Run](https://go.dev/play/p/zIflQgZNuxD) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + isOsWindows := system.IsWindows() + fmt.Println(isOsWindows) +} +``` + +### IsLinux + +

Check if current os is linux.

+ +Signature: + +```go +func IsLinux() bool +``` + +Example:[Run](https://go.dev/play/p/zIflQgZNuxD) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + isOsLinux := system.IsLinux() + fmt.Println(isOsLinux) +} +``` + +### IsMac + +

Check if current os is macos.

+ +Signature: + +```go +func IsMac() bool +``` + +Example:[Run](https://go.dev/play/p/Mg4Hjtyq7Zc) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + isOsMac := system.IsMac() + fmt.Println(isOsMac) +} +``` + +### GetOsEnv + +

Gets the value of the environment variable named by the key.

+ +Signature: + +```go +func GetOsEnv(key string) string +``` + +Example:[Run](https://go.dev/play/p/D88OYVCyjO-) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + err := system.SetOsEnv("foo", "abc") + result := system.GetOsEnv("foo") + + fmt.Println(err) + fmt.Println(result) + // Output: + // + // abc +} +``` + +### SetOsEnv + +

Sets the value of the environment variable named by the key.

+ +Signature: + +```go +func SetOsEnv(key, value string) error +``` + +Example:[Run](https://go.dev/play/p/D88OYVCyjO-) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + err := system.SetOsEnv("foo", "abc") + result := system.GetOsEnv("foo") + + fmt.Println(err) + fmt.Println(result) + // Output: + // + // abc +} +``` + +### RemoveOsEnv + +

Remove a single environment variable.

+ +Signature: + +```go +func RemoveOsEnv(key string) error +``` + +Example:[Run](https://go.dev/play/p/fqyq4b3xUFQ) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + err1 := system.SetOsEnv("foo", "abc") + result1 := GetOsEnv("foo") + + err2 := system.RemoveOsEnv("foo") + result2 := GetOsEnv("foo") + + fmt.Println(err1) + fmt.Println(err2) + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // + // + // abc + // +} +``` + +### CompareOsEnv + +

Get env named by the key and compare it with comparedEnv.

+ +Signature: + +```go +func CompareOsEnv(key, comparedEnv string) bool +``` + +Example:[Run](https://go.dev/play/p/BciHrKYOHbp) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + err := system.SetOsEnv("foo", "abc") + if err != nil { + return + } + + result := system.CompareOsEnv("foo", "abc") + + fmt.Println(result) + + // Output: + // true +} +``` + +### ExecCommand + +

Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.

+ +Signature: + +```go +type ( + Option func(*exec.Cmd) +) +func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error) +``` + +Example:[Run](https://go.dev/play/p/n-2fLyZef-4) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + // linux or mac + stdout, stderr, err := system.ExecCommand("ls") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + assert.Equal("", stderr) + + // windows + stdout, stderr, err = system.ExecCommand("dir") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + + // error command + stdout, stderr, err = system.ExecCommand("abc") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + if err != nil { + fmt.Println(err.Error()) + } +} +``` + +### GetOsBits + +

Get current os bits, 32bit or 64bit. return 32 or 64

+ +Signature: + +```go +func GetOsBits() int +``` + +Example:[Run](https://go.dev/play/p/ml-_XH3gJbW) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/system" +) + +func main() { + osBit := system.GetOsBits() + fmt.Println(osBit) // 32 or 64 +} +``` diff --git a/docs/en/api/packages/tuple.md b/docs/en/api/packages/tuple.md new file mode 100644 index 0000000..2936567 --- /dev/null +++ b/docs/en/api/packages/tuple.md @@ -0,0 +1,1198 @@ +# Tuple + +tuple package implements tuple data type and some operations on it. + +
+ +## Source: + +- [https://github.com/duke-git/lancet/blob/main/tuple/tuple.go](https://github.com/duke-git/lancet/blob/main/tuple/tuple.go) + +
+ +## Usage: + +```go +import ( + "github.com/duke-git/lancet/v2/pointer" +) +``` + +
+ +## Index + +- [Tuple2](#Tuple2) +- [Tuple2_Unbox](#Tuple2_Unbox) +- [Zip2](#Zip2) +- [Unzip2](#Unzip2) +- [Tuple3](#Tuple3) +- [Tuple3_Unbox](#Tuple3_Unbox) +- [Zip3](#Zip3) +- [Unzip3](#Unzip3) +- [Tuple4](#Tuple4) +- [Tuple4_Unbox](#Tuple4_Unbox) +- [Zip4](#Zip4) +- [Unzip4](#Unzip4) +- [Tuple5](#Tuple5) +- [Tuple5_Unbox](#Tuple5_Unbox) +- [Zip5](#Zip5) +- [Unzip5](#Unzip5) +- [Tuple6](#Tuple6) +- [Tuple6_Unbox](#Tuple6_Unbox) +- [Zip6](#Zip6) +- [Unzip6](#Unzip6) +- [Tuple7](#Tuple7) +- [Tuple7_Unbox](#Tuple7_Unbox) +- [Zip7](#Zip7) +- [Unzip7](#Unzip7) +- [Tuple8](#TTuple8uple6) +- [Tuple8_Unbox](#Tuple8_Unbox) +- [Zip8](#Zip8) +- [Unzip8](#Unzip8) +- [Tuple9](#Tuple9) +- [Tuple9_Unbox](#Tuple9_Unbox) +- [Zip9](#Zip9) +- [Unzip9](#Unzip9) +- [Tuple10](#Tuple10) +- [Tuple10_Unbox](#Tuple10_Unbox) +- [Zip10](#Zip10) +- [Unzip10](#Unzip10) + +
+ + +## Documentation + +### Tuple2 + +

Tuple2 represents a 2 elemnets tuple.

+ +Signature: + +```go +type Tuple2[A any, B any] struct { + FieldA A + FieldB B +} + +func NewTuple2[A any, B any](a A, b B) Tuple2[A, B] +``` + +Example:[Run](https://go.dev/play/p/3sHVqBQpLYN) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple2(1, 0.1) + fmt.Printf("%v %v", t.FieldA, t.FieldB) + + // Output: 1 0.1 +} +``` + +### Tuple2_Unbox + +

Tuple2 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple2[A, B]) Unbox() (A, B) +``` + +Example:[Run](https://go.dev/play/p/0fD1qfCVwjm) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple2(1, 0.1) + v1, v2 := t.Unbox() + fmt.Printf("%v %v", v1, v2) + + // Output: 1 0.1 +} +``` + +### Zip2 + +

Create a slice of Tuple2, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip2[A any, B any](a []A, b []B) []Tuple2[A, B] +``` + +Example:[Run](https://go.dev/play/p/4ncWJJ77Xio) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip2([]int{1}, []float64{0.1}) + fmt.Println(result) + + // Output: [{1 0.1}] +} +``` + +### Unzip2 + +

Create a group of slice from a slice of Tuple2.

+ +Signature: + +```go +func Unzip2[A any, B any](tuples []Tuple2[A, B]) ([]A, []B) +``` + +Example:[Run](https://go.dev/play/p/KBecr60feXb) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2 := tuple.Unzip2([]tuple.Tuple2[int, float64]{{FieldA: 1, FieldB: 0.1}}) + + fmt.Printf("%v %v", v1, v2) + + // Output: [1] [0.1] +} +``` + +### Tuple3 + +

Tuple3 represents a 3 elemnets tuple.

+ +Signature: + +```go +type Tuple3[A any, B any, C any] struct { + FieldA A + FieldB B + FieldC C +} + +func NewTuple3[A any, B any, C any](a A, b B c C) Tuple3[A, B, C] +``` + +Example:[Run](https://go.dev/play/p/FtH2sdCLlCf) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple3(1, 0.1, "a") + fmt.Printf("%v %v %v", t.FieldA, t.FieldB, t.FieldC) + + // Output: 1 0.1 a +} +``` + +### Tuple3_Unbox + +

Tuple3 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple3[A, B, C]) Unbox() (A, B, C) +``` + +Example:[Run](https://go.dev/play/p/YojLy-id1BS) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple3(1, 0.1, "a") + v1, v2, v3 := t.Unbox() + fmt.Printf("%v %v %v", v1, v2, v3) + + // Output: 1 0.1 a +} +``` + +### Zip3 + +

Create a slice of Tuple3, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip3[A any, B any, C any](a []A, b []B, c []C) []Tuple3[A, B, C] +``` + +Example:[Run](https://go.dev/play/p/97NgmsTILfu) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip3([]int{1}, []float64{0.1}, []string{"a"}) + fmt.Println(result) + + // Output: [{1 0.1 a}] +} +``` + +### Unzip3 + +

Create a group of slice from a slice of Tuple3.

+ +Signature: + +```go +func Unzip3[A any, B any, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C) +``` + +Example:[Run](https://go.dev/play/p/bba4cpAa7KO) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3 := tuple.Unzip3([]tuple.Tuple3[int, float64, string]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a"}, + }) + + fmt.Printf("%v %v %v", v1, v2, v3) + + // Output: [1] [0.1] [a] +} +``` + +### Tuple4 + +

Tuple4 represents a 4 elemnets tuple.

+ +Signature: + +```go +type Tuple4[A any, B any, C any, D any] struct { + FieldA A + FieldB B + FieldC C + FieldD D +} + +func NewTuple4[A any, B any, C any, D any](a A, b B, c C, d D) Tuple4[A, B, C, D] +``` + +Example:[Run](https://go.dev/play/p/D2EqDz096tk) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple4(1, 0.1, "a", true) + fmt.Printf("%v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD) + + // Output: 1 0.1 a true +} +``` + +### Tuple4_Unbox + +

Tuple4 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple4[A, B, C, D]) Unbox() (A, B, C, D) +``` + +Example:[Run](https://go.dev/play/p/ACj9YuACGgW) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple4(1, 0.1, "a", true) + v1, v2, v3, v4 := t.Unbox() + fmt.Printf("%v %v %v %v", v1, v2, v3, v4) + + // Output: 1 0.1 a true +} +``` + +### Zip4 + +

Create a slice of Tuple4, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip4[A any, B any, C any, D any](a []A, b []B, c []C, d []D) []Tuple4[A, B, C, D] +``` + +Example:[Run](https://go.dev/play/p/PEmTYVK5hL4) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip4([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}) + fmt.Println(result) + + // Output: [{1 0.1 a true}] +} +``` + +### Unzip4 + +

Create a group of slice from a slice of Tuple4.

+ +Signature: + +```go +func Unzip4[A any, B any, C any, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D) +``` + +Example:[Run](https://go.dev/play/p/rb8z4gyYSRN) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4 := tuple.Unzip4([]tuple.Tuple4[int, float64, string, bool]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true}, + }) + + fmt.Printf("%v %v %v %v", v1, v2, v3, v4) + + // Output: [1] [0.1] [a] [true] +} +``` + +### Tuple5 + +

Tuple5 represents a 5 elemnets tuple.

+ +Signature: + +```go +type Tuple5[A any, B any, C any, D any, E any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E +} + +func NewTuple5[A any, B any, C any, D any, E any](a A, b B, c C, d D, e E) Tuple5[A, B, C, D, E] +``` + +Example:[Run](https://go.dev/play/p/2WndmVxPg-r) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple5(1, 0.1, "a", true, 2) + fmt.Printf("%v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE) + + // Output: 1 0.1 a true 2 +} +``` + +### Tuple5_Unbox + +

Tuple5 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple5[A, B, C, D, E]) Unbox() (A, B, C, D, E) +``` + +Example:[Run](https://go.dev/play/p/GyIyZHjCvoS) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple5(1, 0.1, "a", true, 2) + v1, v2, v3, v4, v5 := t.Unbox() + fmt.Printf("%v %v %v %v %v", v1, v2, v3, v4, v5) + + // Output: 1 0.1 a true 2 +} +``` + +### Zip5 + +

Create a slice of Tuple5, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip5[A any, B any, C any, D any, E any](a []A, b []B, c []C, d []D, e []E) []Tuple5[A, B, C, D, E] +``` + +Example:[Run](https://go.dev/play/p/fCAAJLMfBIP) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip5([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2}] +} +``` + +### Unzip5 + +

Create a group of slice from a slice of Tuple5.

+ +Signature: + +```go +func Unzip5[A any, B any, C any, D any, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, []D, []E) +``` + +Example:[Run](https://go.dev/play/p/gyl6vKfhqPb) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5 := tuple.Unzip5([]tuple.Tuple5[int, float64, string, bool, int]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2}, + }) + + fmt.Printf("%v %v %v %v %v", v1, v2, v3, v4, v5) + + // Output: [1] [0.1] [a] [true] [2] +} +``` + +### Tuple6 + +

Tuple6 represents a 6 elemnets tuple.

+ +Signature: + +```go +type Tuple6[A any, B any, C any, D any, E any, F any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F +} + +func NewTuple6[A any, B any, C any, D any, E any, F any](a A, b B, c C, d D, e E, f F) Tuple6[A, B, C, D, E, F] +``` + +Example:[Run](https://go.dev/play/p/VjqcCwEJZbs) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple6(1, 0.1, "a", true, 2, 2.2) + fmt.Printf("%v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF) + + // Output: 1 0.1 a true 2 2.2 +} +``` + +### Tuple6_Unbox + +

Tuple6 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple6[A, B, C, D, E, F]) Unbox() (A, B, C, D, E, F) +``` + +Example:[Run](https://go.dev/play/p/FjIHV7lpxmW) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple6(1, 0.1, "a", true, 2, 2.2) + v1, v2, v3, v4, v5, v6 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v", v1, v2, v3, v4, v5, v6) + + // Output: 1 0.1 a true 2 2.2 +} +``` + +### Zip6 + +

Create a slice of Tuple6, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip6[A any, B any, C any, D any, E any, F any](a []A, b []B, c []C, d []D, e []E, f []F) []Tuple6[A, B, C, D, E, F] +``` + +Example:[Run](https://go.dev/play/p/oWPrnUYuFHo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip6([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2}] +} +``` + +### Unzip6 + +

Create a group of slice from a slice of Tuple6.

+ +Signature: + +```go +func Unzip6[A any, B any, C any, D any, E any, F any](tuples []Tuple6[A, B, C, D, E, F]) ([]A, []B, []C, []D, []E, []F) +``` + +Example:[Run](https://go.dev/play/p/l41XFqCyh5E) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6 := tuple.Unzip6([]tuple.Tuple6[int, float64, string, bool, int, float32]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2}, + }) + + fmt.Printf("%v %v %v %v %v %v", v1, v2, v3, v4, v5, v6) + + // Output: [1] [0.1] [a] [true] [2] [2.2] +} +``` + +### Tuple7 + +

Tuple7 represents a 7 elemnets tuple.

+ +Signature: + +```go +type Tuple7[A any, B any, C any, D any, E any, F any, G any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F + FieldG G +} + +func NewTuple7[A any, B any, C any, D any, E any, F any, G any](a A, b B, c C, d D, e E, f F, g G) Tuple7[A, B, C, D, E, F, G] +``` + +Example:[Run](https://go.dev/play/p/dzAgv_Ezub9) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple7(1, 0.1, "a", true, 2, 2.2, "b") + fmt.Printf("%v %v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF, t.FieldG) + + // Output: 1 0.1 a true 2 2.2 b +} +``` + +### Tuple7_Unbox + +

Tuple7 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple7[A, B, C, D, E, F, G]) Unbox() (A, B, C, D, E, F, G) +``` + +Example:[Run](https://go.dev/play/p/R9I8qeDk0zs) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple7(1, 0.1, "a", true, 2, 2.2, "b") + v1, v2, v3, v4, v5, v6, v7 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7) + + // Output: 1 0.1 a true 2 2.2 b +} +``` + +### Zip7 + +

Create a slice of Tuple7, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip7[A any, B any, C any, D any, E any, F any, G any](a []A, b []B, c []C, d []D, e []E, f []F, g []G) []Tuple7[A, B, C, D, E, F, G] +``` + +Example:[Run](https://go.dev/play/p/WUJuo897Egf) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip7([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}, []string{"b"}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2 b}] +} +``` + +### Unzip7 + +

Create a group of slice from a slice of Tuple7.

+ +Signature: + +```go +func Unzip7[A any, B any, C any, D any, E any, F any, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, []B, []C, []D, []E, []F, []G) +``` + +Example:[Run](https://go.dev/play/p/hws_P1Fr2j3) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6, v7 := tuple.Unzip7([]tuple.Tuple7[int, float64, string, bool, int, float32, string]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2, FieldG: "b"}, + }) + + fmt.Printf("%v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7) + + // Output: [1] [0.1] [a] [true] [2] [2.2] [b] +} +``` + +### Tuple8 + +

Tuple8 represents a 8 elemnets tuple.

+ +Signature: + +```go +type Tuple8[A any, B any, C any, D any, E any, F any, G any, H any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F + FieldG G + FieldH H +} + +func NewTuple8[A any, B any, C any, D any, E any, F any, G any, H any](a A, b B, c C, d D, e E, f F, g G, h H) Tuple8[A, B, C, D, E, F, G, H] +``` + +Example:[Run](https://go.dev/play/p/YA9S0rz3dRz) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple8(1, 0.1, "a", true, 2, 2.2, "b", "c") + fmt.Printf("%v %v %v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF, t.FieldG, t.FieldH) + + // Output: 1 0.1 a true 2 2.2 b c +} +``` + +### Tuple8_Unbox + +

Tuple8 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple8[A, B, C, D, E, F, G, H]) Unbox() (A, B, C, D, E, F, G, H) +``` + +Example:[Run](https://go.dev/play/p/PRxLBBb4SMl) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple8(1, 0.1, "a", true, 2, 2.2, "b", "c") + v1, v2, v3, v4, v5, v6, v7, v8 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8) + + // Output: 1 0.1 a true 2 2.2 b c +} +``` + +### Zip8 + +

Create a slice of Tuple8, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip8[A any, B any, C any, D any, E any, F any, G any, H any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H) []Tuple8[A, B, C, D, E, F, G, H] +``` + +Example:[Run](https://go.dev/play/p/8V9jWkuJfaQ) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip8([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}, []string{"b"}, []string{"c"}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2 b c}] +} +``` + +### Unzip8 + +

Create a group of slice from a slice of Tuple8.

+ +Signature: + +```go +func Unzip8[A any, B any, C any, D any, E any, F any, G any, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) ([]A, []B, []C, []D, []E, []F, []G, []H) +``` + +Example:[Run](https://go.dev/play/p/1SndOwGsZB4) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6, v7, v8 := tuple.Unzip8([]tuple.Tuple8[int, float64, string, bool, int, float32, string, string]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2, FieldG: "b", FieldH: "c"}, + }) + + fmt.Printf("%v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8) + + // Output: [1] [0.1] [a] [true] [2] [2.2] [b] [c] +} +``` + +### Tuple9 + +

Tuple9 represents a 9 elemnets tuple.

+ +Signature: + +```go + +type Tuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F + FieldG G + FieldH H + FieldI I +} + +func NewTuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) Tuple9[A, B, C, D, E, F, G, H, I] + +``` + +Example:[Run](https://go.dev/play/p/yS2NGGtZpQr) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple9(1, 0.1, "a", true, 2, 2.2, "b", "c", map[string]int{"a": 1}) + fmt.Printf("%v %v %v %v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF, t.FieldG, t.FieldH, t.FieldI) + + // Output: 1 0.1 a true 2 2.2 b c map[a:1] +} +``` + +### Tuple9_Unbox + +

Tuple9 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple9[A, B, C, D, E, F, G, H, I]) Unbox() (A, B, C, D, E, F, G, H, I) +``` + +Example:[Run](https://go.dev/play/p/oFJFGTAuOa8) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + t := tuple.NewTuple9(1, 0.1, "a", true, 2, 2.2, "b", "c", map[string]int{"a": 1}) + v1, v2, v3, v4, v5, v6, v7, v8, v9 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8, v9) + + // Output: 1 0.1 a true 2 2.2 b c map[a:1] +} +``` + +### Zip9 + +

Create a slice of Tuple9, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip9[A any, B any, C any, D any, E any, F any, G any, H any, I any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I) []Tuple9[A, B, C, D, E, F, G, H, I] +``` + +Example:[Run](https://go.dev/play/p/cgsL15QYnfz) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip9([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}, []string{"b"}, []string{"c"}, []int64{3}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2 b c 3}] +} +``` + +### Unzip9 + +

Create a group of slice from a slice of Tuple9.

+ +Signature: + +```go +func Unzip9[A any, B any, C any, D any, E any, F any, G any, H any, I any](tuples []Tuple9[A, B, C, D, E, F, G, H, I]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I) +``` + +Example:[Run](https://go.dev/play/p/91-BU_KURSA) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6, v7, v8, v9 := tuple.Unzip9([]tuple.Tuple9[int, float64, string, bool, int, float32, string, string, int64]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2, FieldG: "b", FieldH: "c", FieldI: 3}, + }) + + fmt.Printf("%v %v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8, v9) + + // Output: [1] [0.1] [a] [true] [2] [2.2] [b] [c] [3] +} +``` + +### Tuple10 + +

Tuple10 represents a 10 elemnets tuple.

+ +Signature: + +```go + +type Tuple10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any] struct { + FieldA A + FieldB B + FieldC C + FieldD D + FieldE E + FieldF F + FieldG G + FieldH H + FieldI I + FieldJ J +} + +func NewTuple10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any](a A, b B, c C, d D, e E, f F, g G, h H, i I, j J) Tuple10[A, B, C, D, E, F, G, H, I, J] + +``` + +Example:[Run](https://go.dev/play/p/799qqZg0hUv) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + type foo struct { + A string + } + t := tuple.NewTuple10(1, 0.1, "a", true, 2, 2.2, "b", "c", map[string]int{"a": 1}, foo{A: "a"}) + fmt.Printf("%v %v %v %v %v %v %v %v %v %v", t.FieldA, t.FieldB, t.FieldC, t.FieldD, t.FieldE, t.FieldF, t.FieldG, t.FieldH, t.FieldI, t.FieldJ) + + // Output: 1 0.1 a true 2 2.2 b c map[a:1] {a} +} +``` + +### Tuple10_Unbox + +

Tuple10 Unbox returns values in tuple.

+ +Signature: + +```go +func (t Tuple10[A, B, C, D, E, F, G, H, I, J]) Unbox() (A, B, C, D, E, F, G, H, I, J) +``` + +Example:[Run](https://go.dev/play/p/qfyx3x_X0Cu) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + type foo struct { + A string + } + t := tuple.NewTuple10(1, 0.1, "a", true, 2, 2.2, "b", "c", map[string]int{"a": 1}, foo{A: "a"}) + v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 := t.Unbox() + fmt.Printf("%v %v %v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) + + // Output: 1 0.1 a true 2 2.2 b c map[a:1] {a} +} +``` + +### Zip10 + +

Create a slice of Tuple10, whose elements are correspond to the given slice elements.

+ +Signature: + +```go +func Zip10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I, j []J) []Tuple10[A, B, C, D, E, F, G, H, I, J] +``` + +Example:[Run](https://go.dev/play/p/YSR-2cXnrY4) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + result := tuple.Zip10([]int{1}, []float64{0.1}, []string{"a"}, []bool{true}, []int{2}, []float32{2.2}, []string{"b"}, []string{"c"}, []int64{3}, []bool{false}) + fmt.Println(result) + + // Output: [{1 0.1 a true 2 2.2 b c 3 false}] +} +``` + +### Unzip10 + +

Create a group of slice from a slice of Tuple10.

+ +Signature: + +```go +func Unzip10[A any, B any, C any, D any, E any, F any, G any, H any, I any, J any](tuples []Tuple10[A, B, C, D, E, F, G, H, I, J]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I, []J) +``` + +Example:[Run](https://go.dev/play/p/-taQB6Wfre_z) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/tuple" +) + +func main() { + v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 := tuple.Unzip10([]tuple.Tuple10[int, float64, string, bool, int, float32, string, string, int64, bool]{ + {FieldA: 1, FieldB: 0.1, FieldC: "a", FieldD: true, FieldE: 2, FieldF: 2.2, FieldG: "b", FieldH: "c", FieldI: 3, FieldJ: false}, + }) + + fmt.Printf("%v %v %v %v %v %v %v %v %v %v", v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) + + // Output: [1] [0.1] [a] [true] [2] [2.2] [b] [c] [3] [false] +} +``` diff --git a/docs/en/api/packages/validator.md b/docs/en/api/packages/validator.md new file mode 100644 index 0000000..58a5df0 --- /dev/null +++ b/docs/en/api/packages/validator.md @@ -0,0 +1,1190 @@ +# Validator + +Package validator contains some functions for data validation. + +
+ +## Source: + +- [https://github.com/duke-git/lancet/blob/main/validator/validator.go](https://github.com/duke-git/lancet/blob/main/validator/validator.go) + +
+ +## Usage: + +```go +import ( + "github.com/duke-git/lancet/v2/validator" +) +``` + +
+ +## Index + +- [ContainChinese](#ContainChinese) +- [ContainLetter](#ContainLetter) +- [ContainLower](#ContainLower) +- [ContainUpper](#ContainUpper) +- [IsAlpha](#IsAlpha) +- [IsAllUpper](#IsAllUpper) +- [IsAllLower](#IsAllLower) +- [IsASCII](#IsASCII) +- [IsBase64](#IsBase64) +- [IsChineseMobile](#IsChineseMobile) +- [IsChineseIdNum](#IsChineseIdNum) +- [IsChinesePhone](#IsChinesePhone) +- [IsCreditCard](#IsCreditCard) +- [IsDns](#IsDns) +- [IsEmail](#IsEmail) +- [IsEmptyString](#IsEmptyString) +- [IsInt](#IsInt) +- [IsFloat](#IsFloat) +- [IsNumber](#IsNumber) +- [IsIntStr](#IsIntStr) +- [IsFloatStr](#IsFloatStr) +- [IsNumberStr](#IsNumberStr) +- [IsJSON](#IsJSON) +- [IsRegexMatch](#IsRegexMatch) +- [IsIp](#IsIp) +- [IsIpV4](#IsIpV4) +- [IsIpV6](#IsIpV6) +- [IsStrongPassword](#IsStrongPassword) +- [IsUrl](#IsUrl) +- [IsWeakPassword](#IsWeakPassword) +- [IsZeroValue](#IsZeroValue) +- [IsGBK](#IsGBK) +- [IsPrintable](#IsPrintable) + +
+ + + +## Documentation + +### ContainChinese + +

Check if the string contain mandarin chinese.

+ +Signature: + +```go +func ContainChinese(s string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.ContainChinese("你好") + result2 := validator.ContainChinese("你好hello") + result3 := validator.ContainChinese("hello") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### ContainLetter + +

Check if the string contain at least one letter.

+ +Signature: + +```go +func ContainLetter(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.ContainLetter("你好") + result2 := validator.ContainLetter("&@#$%^&*") + result3 := validator.ContainLetter("ab1") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // false + // false + // true +} +``` + +### ContainLower + +

Check if the string contain at least one lower case letter a-z.

+ +Signature: + +```go +func ContainLower(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.ContainLower("abc") + result2 := validator.ContainLower("aBC") + result3 := validator.ContainLower("ABC") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### ContainUpper + +

Check if the string contain at least one upper case letter A-Z.

+ +Signature: + +```go +func ContainUpper(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.ContainUpper("ABC") + result2 := validator.ContainUpper("abC") + result3 := validator.ContainUpper("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### IsAlpha + +

Check if the string contains only letters (a-zA-Z).

+ +Signature: + +```go +func IsAlpha(s string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsAlpha("abc") + result2 := validator.IsAlpha("ab1") + result3 := validator.IsAlpha("") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsAllUpper + +

Check if string is all upper case letters A-Z.

+ +Signature: + +```go +func IsAllUpper(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsAllUpper("ABC") + result2 := validator.IsAllUpper("ABc") + result3 := validator.IsAllUpper("AB1") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsAllLower + +

Check if string is all lower case letters a-z.

+ +Signature: + +```go +func IsAllLower(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsAllLower("abc") + result2 := validator.IsAllLower("abC") + result3 := validator.IsAllLower("ab1") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsASCII + +

Checks if string is all ASCII char.

+ +Signature: + +```go +func IsASCII(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsASCII("ABC") + result2 := validator.IsASCII("123") + result3 := validator.IsASCII("") + result4 := validator.IsASCII("😄") + result5 := validator.IsASCII("你好") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // true + // true + // true + // false + // false +} +``` + +### IsBase64 + +

Check if the string is base64 string.

+ +Signature: + +```go +func IsBase64(base64 string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsBase64("aGVsbG8=") + result2 := validator.IsBase64("123456") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsChineseMobile + +

Check if the string is valid chinese mobile number.

+ +Signature: + +```go +func IsChineseMobile(mobileNum string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsChineseMobile("13263527980") + result2 := validator.IsChineseMobile("434324324") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsChineseIdNum + +

Check if the string is chinese id number.

+ +Signature: + +```go +func IsChineseIdNum(id string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsChineseIdNum("210911192105130715") + result2 := validator.IsChineseIdNum("123456") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsChinesePhone + +

Check if the string is chinese phone number.

+ +Signature: + +```go +func IsChinesePhone(phone string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsChinesePhone("010-32116675") + result2 := validator.IsChinesePhone("123-87562") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsCreditCard + +

Check if the string is credit card.

+ +Signature: + +```go +func IsCreditCard(creditCart string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsCreditCard("4111111111111111") + result2 := validator.IsCreditCard("123456") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsDns + +

Check if the string is valid dns.

+ +Signature: + +```go +func IsDns(dns string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsDns("abc.com") + result2 := validator.IsDns("a.b.com") + result3 := validator.IsDns("http://abc.com") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsEmail + +

Check if the string is email address.

+ +Signature: + +```go +func IsEmail(email string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsEmail("abc@xyz.com") + result2 := validator.IsEmail("a.b@@com") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsEmptyString + +

Check if the string is empty or not.

+ +Signature: + +```go +func IsEmptyString(s string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsEmptyString("") + result2 := validator.IsEmptyString(" ") + result3 := validator.IsEmptyString("\t") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsInt + +

Check if the value is integer(int, unit) or not.

+ +Signature: + +```go +func IsInt(v any) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsInt("") + result2 := validator.IsInt("3") + result3 := validator.IsInt(0.1) + result4 := validator.IsInt(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} +``` + +### IsFloat + +

Check if the value is float(float32, float34) or not.

+ +Signature: + +```go +func IsFloat(v any) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsFloat("") + result2 := validator.IsFloat("3") + result3 := validator.IsFloat(0) + result4 := validator.IsFloat(0.1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} +``` + +### IsNumber + +

Check if the value is number(integer, float) or not.

+ +Signature: + +```go +func IsNumber(v any) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsNumber("") + result2 := validator.IsNumber("3") + result3 := validator.IsNumber(0.1) + result4 := validator.IsNumber(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // true + // true +} +``` + +### IsIntStr + +

Check if the string can convert to a integer.

+ +Signature: + +```go +func IsIntStr(s string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIntStr("+3") + result2 := validator.IsIntStr("-3") + result3 := validator.IsIntStr("3.") + result4 := validator.IsIntStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsFloatStr + +

Check if the string can convert to a float.

+ +Signature: + +```go +func IsFloatStr(s string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsFloatStr("3.") + result2 := validator.IsFloatStr("+3.") + result3 := validator.IsFloatStr("12") + result4 := validator.IsFloatStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false +} +``` + +### IsNumberStr + +

Check if the string can convert to a number.

+ +Signature: + +```go +func IsNumberStr(s string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsNumberStr("3.") + result2 := validator.IsNumberStr("+3.") + result3 := validator.IsNumberStr("+3e2") + result4 := validator.IsNumberStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false +} +``` + +### IsJSON + +

Check if the string is valid JSON.

+ +Signature: + +```go +func IsJSON(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsJSON("{}") + result2 := validator.IsJSON("{\"name\": \"test\"}") + result3 := validator.IsJSON("") + result4 := validator.IsJSON("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsRegexMatch + +

Check if the string match the regexp.

+ +Signature: + +```go +func IsRegexMatch(s, regex string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsRegexMatch("abc", `^[a-zA-Z]+$`) + result2 := validator.IsRegexMatch("ab1", `^[a-zA-Z]+$`) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsIp + +

Check if the string is a ip address.

+ +Signature: + +```go +func IsIp(ipstr string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIp("127.0.0.1") + result2 := validator.IsIp("::0:0:0:0:0:0:1") + result3 := validator.IsIp("127.0.0") + result4 := validator.IsIp("::0:0:0:0:") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsIpV4 + +

Check if the string is a ipv4 address.

+ +Signature: + +```go +func IsIpV4(ipstr string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIpV4("127.0.0.1") + result2 := validator.IsIpV4("::0:0:0:0:0:0:1") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsIpV6 + +

Check if the string is a ipv6 address.

+ +Signature: + +```go +func IsIpV6(ipstr string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIpV6("127.0.0.1") + result2 := validator.IsIpV6("::0:0:0:0:0:0:1") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true +} +``` + +### IsStrongPassword + +

Check if the string is strong password (alpha(lower+upper) + number + special chars(!@#$%^&*()?><)).

+ +Signature: + +```go +func IsStrongPassword(password string, length int) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsStrongPassword("abcABC", 6) + result2 := validator.IsStrongPassword("abcABC123@#$", 10) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true +} +``` + +### IsUrl + +

Check if the string is url.

+ +Signature: + +```go +func IsUrl(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsUrl("abc.com") + result2 := validator.IsUrl("http://abc.com") + result3 := validator.IsUrl("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### IsWeakPassword + +

Checks if the string is weak password(only letter or only number or letter + number) +.

+ +Signature: + +```go +func IsWeakPassword(password string, length int) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsWeakPassword("abcABC") + result2 := validator.IsWeakPassword("abc123@#$") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsZeroValue + +

Checks if passed value is a zero value.

+ +Signature: + +```go +func IsZeroValue(value any) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsZeroValue("") + result2 := validator.IsZeroValue(0) + result3 := validator.IsZeroValue("abc") + result4 := validator.IsZeroValue(1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsGBK + +

Checks if data encoding is gbk(Chinese character internal code extension specification). this function is implemented by whether double bytes fall within the encoding range of gbk,while each byte of utf-8 encoding format falls within the encoding range of gbk.Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding and then call IsGBK() to check gbk encoding. like the example.

+ +Signature: + +```go +func IsGBK(data []byte) bool +``` + +Example: + +```go +import ( + "fmt" + "golang.org/x/text/encoding/simplifiedchinese" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + str := "你好" + gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str)) + + result := validator.IsGBK(gbkData) + + fmt.Println(result) + + // Output: + // true +} +``` + +### IsPrintable + +

Checks if string is all printable chars.

+ +Signature: + +```go +func IsPrintable(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsPrintable("ABC") + result2 := validator.IsPrintable("{id: 123}") + result3 := validator.IsPrintable("") + result4 := validator.IsPrintable("😄") + result5 := validator.IsPrintable("\u0000") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // true + // true + // true + // true + // false +} +``` diff --git a/docs/en/api/packages/xerror.md b/docs/en/api/packages/xerror.md new file mode 100644 index 0000000..428d4b0 --- /dev/null +++ b/docs/en/api/packages/xerror.md @@ -0,0 +1,489 @@ +# Xerror + +Package xerror implements helpers for errors. + +
+ +## Source: + +- [https://github.com/duke-git/lancet/blob/main/xerror/xerror.go](https://github.com/duke-git/lancet/blob/main/xerror/xerror.go) + +
+ +## Usage: + +```go +import ( + "github.com/duke-git/lancet/v2/xerror" +) +``` + +
+ +## Index + +- [New](#New) +- [Wrap](#Wrap) +- [Unwrap](#Unwrap) +- [XError_Wrap](#XError_Wrap) +- [XError_Unwrap](#XError_Unwrap) +- [XError_With](#XError_With) +- [XError_Is](#XError_Is) +- [XError_Id](#XError_Id) +- [XError_Values](#XError_Values) +- [XError_StackTrace](#XError_StackTrace) +- [XError_Info](#XError_Info) +- [XError_Error](#XError_Error) +- [TryUnwrap](#TryUnwrap) + +
+ +## Documentation + +### New + +

Creates a new XError pointer instance with message.

+ +Signature: + +```go +type XError struct { + id string + message string + stack *stack + cause error + values map[string]any +} + +func New(format string, args ...any) *XError +``` + +Example:[Run](https://go.dev/play/p/w4oWZts7q7f) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error") + fmt.Println(err.Error()) + + // Output: + // error +} +``` + +### Wrap + +

Creates a new XError pointer instance based on error object, and add message.

+ +Signature: + +```go +func Wrap(cause error, message ...any) *XError +``` + +Example:[Run](https://go.dev/play/p/5385qT2dCi4) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("wrong password") + wrapErr := xerror.Wrap(err, "error") + + fmt.Println(wrapErr.Error()) + + // Output: + // error: wrong password +} +``` + +### Unwrap + +

Returns unwrapped XError from err by errors.As. If no XError, returns nil.

+ +Signature: + +```go +func Unwrap(err error) *XError +``` + +Example:[Run](https://go.dev/play/p/LKMLep723tu) + +```go +package main + +import ( + "fmt" + "github.com/pkg/errors" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").With("level", "high") + wrapErr := errors.Wrap(err1, "oops") + + err := xerror.Unwrap(wrapErr) + + values := err.Values() + fmt.Println(values["level"]) + + // Output: + // high +} +``` + +### XError_Wrap + +

Creates a new XError and copy message and id to new one.

+ +Signature: + +```go +func (e *XError) Wrap(cause error) *XError +``` + +Example:[Run](https://go.dev/play/p/RpjJ5u5sc97) + +```go +package main + +import ( + "fmt" + "errors" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").With("level", "high") + err2 := err1.Wrap(errors.New("invalid username")) + + fmt.Println(err2.Error()) + + // Output: + // error: invalid username +} +``` + +### XError_Unwrap + +

Compatible with github.com/pkg/errors.

+ +Signature: + +```go +func (e *XError) Unwrap() error +``` + +Example:[Run](https://go.dev/play/p/VUXJ8BST4c6) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").With("level", "high") + err2 := err1.Wrap(errors.New("invalid username")) + + err := err2.Unwrap() + + fmt.Println(err.Error()) + + // Output: + // invalid username +} +``` + +### XError_With + +

Adds key and value related to the XError object.

+ +Signature: + +```go +func (e *XError) With(key string, value any) *XError +``` + +Example:[Run](https://go.dev/play/p/ow8UISXX_Dp) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error").With("level", "high") + + errLevel := err.Values()["level"] + + fmt.Println(errLevel) + + // Output: + // high +} +``` + +### XError_Id + +

Sets XError object id to check equality in XError.Is.

+ +Signature: + +```go +func (e *XError) Id(id string) *XError +``` + +Example:[Run](https://go.dev/play/p/X6HBlsy58U9) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").Id("e001") + err2 := xerror.New("error").Id("e001") + err3 := xerror.New("error").Id("e003") + + equal := err1.Is(err2) + notEqual := err1.Is(err3) + + fmt.Println(equal) + fmt.Println(notEqual) + + // Output: + // true + // false +} +``` + +### XError_Is + +

Checks if target error is XError and Error.id of two errors are matched.

+ +Signature: + +```go +func (e *XError) Is(target error) bool +``` + +Example:[Run](https://go.dev/play/p/X6HBlsy58U9) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err1 := xerror.New("error").Id("e001") + err2 := xerror.New("error").Id("e001") + err3 := xerror.New("error").Id("e003") + + equal := err1.Is(err2) + notEqual := err1.Is(err3) + + fmt.Println(equal) + fmt.Println(notEqual) + + // Output: + // true + // false +} +``` + +### XError_Values + +

Returns map of key and value that is set by With. All wrapped xerror.XError key and values will be merged. Key and values of wrapped error is overwritten by upper xerror.XError.

+ +Signature: + +```go +func (e *XError) Values() map[string]any +``` + +Example:[Run](https://go.dev/play/p/ow8UISXX_Dp) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error").With("level", "high") + + errLevel := err.Values()["level"] + + fmt.Println(errLevel) + + // Output: + // high +} +``` + + +### XError_StackTrace + +

Returns stack trace which is compatible with pkg/errors.

+ +Signature: + +```go +func (e *XError) StackTrace() StackTrace +``` + +Example:[Run](https://go.dev/play/p/6FAvSQpa7pc) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error") + + stacks := err.Stacks() + + fmt.Println(stacks[0].Func) + fmt.Println(stacks[0].Line) + + containFile := strings.Contains(stacks[0].File, "xxx.go") + fmt.Println(containFile) +} +``` + + +### XError_Info + +

Returns information of xerror, which can be printed.

+ +Signature: + +```go +func (e *XError) Info() *errInfo +``` + +Example:[Run](https://go.dev/play/p/1ZX0ME1F-Jb) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + cause := errors.New("error") + err := xerror.Wrap(cause, "invalid username").Id("e001").With("level", "high") + + errInfo := err.Info() + + fmt.Println(errInfo.Id) + fmt.Println(errInfo.Cause) + fmt.Println(errInfo.Values["level"]) + fmt.Println(errInfo.Message) + + // Output: + // e001 + // error + // high + // invalid username +} +``` + + +### XError_Error + +

Error implements standard error interface.

+ +Signature: + +```go +func (e *XError) Error() string +``` + +Example:[Run](https://go.dev/play/p/w4oWZts7q7f) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + err := xerror.New("error") + fmt.Println(err.Error()) + + // Output: + // error +} +``` +### TryUnwrap + +

TryUnwrap if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.

+ +Signature: + +```go +func TryUnwrap[T any](val T, err error) T +``` + +Example:[Run](https://go.dev/play/p/acyZVkNZEeW) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/xerror" +) + +func main() { + result1 := xerror.TryUnwrap(strconv.Atoi("42")) + fmt.Println(result1) + + _, err := strconv.Atoi("4o2") + defer func() { + v := recover() + result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error()) + fmt.Println(result2) + }() + + xerror.TryUnwrap(strconv.Atoi("4o2")) + + // Output: + // 42 + // true +} +``` diff --git a/docs/guide/introduction.md b/docs/guide/introduction.md index 26f931f..a77674a 100644 --- a/docs/guide/introduction.md +++ b/docs/guide/introduction.md @@ -4,7 +4,7 @@ outline: deep # lancet是什么? -lancet(柳叶刀)是一个强大、全面、高效、可复用的go语言工具函数库。lancet受到了java apache common包和lodash.js的启发。 +lancet(柳叶刀)是一个功能强大、全面、高效、可复用的go语言工具函数库。lancet受到了java apache common包和lodash.js的启发。 ## 为什么选择lancet?