diff --git a/.gitignore b/.gitignore index c98433c..48c98f3 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ slice/testdata/* cryptor/*.pem test docs/node_modules -docs/.vitepress/cache \ No newline at end of file +docs/.vitepress/cache +docs/.vitepress/dist \ No newline at end of file diff --git a/docs/en/guide/getting_started.md b/docs/en/guide/getting_started.md index bdd0c72..9eb9e4c 100644 --- a/docs/en/guide/getting_started.md +++ b/docs/en/guide/getting_started.md @@ -47,4 +47,4 @@ func main() { ## More -Check out the [APIs]([API](https://lancet.go.dev/api/overview.html)) for details. +Check out the [APIs](https://github.com/duke-git/lancet) for details. diff --git a/docs/olddocs/stream.md b/docs/olddocs/stream.md deleted file mode 100644 index bb9af2d..0000000 --- a/docs/olddocs/stream.md +++ /dev/null @@ -1,940 +0,0 @@ -# Stream - -Package stream implements a sequence of elements supporting sequential and operations. This package is an experiment to explore if stream in go can work as the way java does. it's feature is very limited. - -
- -## Source: - -- [https://github.com/duke-git/lancet/blob/main/stream/stream.go](https://github.com/duke-git/lancet/blob/main/stream/stream.go) - - - -## Usage: - -```go -import ( - "github.com/duke-git/lancet/v2/stream" -) -``` - - - -## Index - -- [Of](#Of) -- [FromSlice](#FromSlice) -- [FromChannel](#FromChannel) -- [FromRange](#FromRange) -- [Generate](#Generate) -- [Concat](#Concat) -- [Distinct](#Distinct) -- [Filter](#Filter) -- [Map](#Map) -- [Peek](#Peek) -- [Skip](#Skip) -- [Limit](#Limit) -- [Reverse](#Reverse) -- [Range](#Range) -- [Sorted](#Sorted) -- [ForEach](#ForEach) -- [Reduce](#Reduce) -- [FindFirst](#FindFirst) -- [FindLast](#FindLast) -- [Max](#Max) -- [Min](#Min) -- [AllMatch](#AllMatch) -- [AnyMatch](#AnyMatch) -- [NoneMatch](#NoneMatch) -- [Count](#Count) -- [ToSlice](#ToSlice) - - - -## Documentation - -### Of - -Creates a stream whose elements are the specified values.
- -Signature: - -```go -func Of[T any](elems ...T) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s := stream.Of(1, 2, 3) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` - -### FromSlice - -Creates a stream from slice.
- -Signature: - -```go -func FromSlice[T any](source []T) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s := stream.FromSlice([]int{1, 2, 3}) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` - -### FromChannel - -Creates a stream from channel.
- -Signature: - -```go -func FromChannel[T any](source <-chan T) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - ch := make(chan int) - go func() { - for i := 1; i < 4; i++ { - ch <- i - } - close(ch) - }() - - s := stream.FromChannel(ch) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` - -### FromRange - -Creates a number stream from start to end. both start and end are included. [start, end]
- -Signature: - -```go -func FromRange[T constraints.Integer | constraints.Float](start, end, step T) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s := stream.FromRange(1, 5, 1) - - data := s.ToSlice() - fmt.Println(data) - - // Output: - // [1 2 3 4 5] -} -``` - -### Generate - -Creates a stream where each element is generated by the provided generater function.
- -Signature: - -```go -func Generate[T any](generator func() func() (item T, ok bool)) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - n := 0 - max := 4 - - generator := func() func() (int, bool) { - return func() (int, bool) { - n++ - return n, n < max - } - } - - s := stream.Generate(generator) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` - -### Concat - -Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.
- -Signature: - -```go -func Concat[T any](a, b stream[T]) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s1 := stream.FromSlice([]int{1, 2, 3}) - s2 := stream.FromSlice([]int{4, 5, 6}) - - s := Concat(s1, s2) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### Distinct - -Creates returns a stream that removes the duplicated items. Support chainable operation
- -Signature: - -```go -func (s stream[T]) Distinct() stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 2, 3, 3, 3}) - distinct := original.Distinct() - - data1 := original.ToSlice() - data2 := distinct.ToSlice() - - fmt.Println(data1) - fmt.Println(data2) - - // Output: - // [1 2 2 3 3 3] - // [1 2 3] -} -``` - -### Filter - -Returns a stream consisting of the elements of this stream that match the given predicate. Support chainable operation
- -Signature: - -```go -func (s stream[T]) Filter(predicate func(item T) bool) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3, 4, 5}) - - isEven := func(n int) bool { - return n%2 == 0 - } - - even := original.Filter(isEven) - - fmt.Println(even.ToSlice()) - - // Output: - // [2 4] -} -``` - -### Map - -Returns a stream consisting of the elements of this stream that apply the given function to elements of stream. Support chainable operation
- -Signature: - -```go -func (s stream[T]) Map(mapper func(item T) T) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - addOne := func(n int) int { - return n + 1 - } - - increament := original.Map(addOne) - - fmt.Println(increament.ToSlice()) - - // Output: - // [2 3 4] -} -``` - -### Peek - -Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. Support chainable operation
- -Signature: - -```go -func (s stream[T]) Peek(consumer func(item T)) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - data := []string{} - peekStream := original.Peek(func(n int) { - data = append(data, fmt.Sprint("value", n)) - }) - - fmt.Println(original.ToSlice()) - fmt.Println(peekStream.ToSlice()) - fmt.Println(data) - - // Output: - // [1 2 3] - // [1 2 3] - // [value1 value2 value3] -} -``` - -### Skip - -Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned. Support chainable operation
- -Signature: - -```go -func (s stream[T]) Skip(n int) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3, 4}) - - s1 := original.Skip(-1) - s2 := original.Skip(0) - s3 := original.Skip(1) - s4 := original.Skip(5) - - fmt.Println(s1.ToSlice()) - fmt.Println(s2.ToSlice()) - fmt.Println(s3.ToSlice()) - fmt.Println(s4.ToSlice()) - - // Output: - // [1 2 3 4] - // [1 2 3 4] - // [2 3 4] - // [] -} -``` - -### Limit - -Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length. Support chainable operation
- -Signature: - -```go -func (s stream[T]) Limit(maxSize int) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3, 4}) - - s1 := original.Limit(-1) - s2 := original.Limit(0) - s3 := original.Limit(1) - s4 := original.Limit(5) - - fmt.Println(s1.ToSlice()) - fmt.Println(s2.ToSlice()) - fmt.Println(s3.ToSlice()) - fmt.Println(s4.ToSlice()) - - // Output: - // [] - // [] - // [1] - // [1 2 3 4] -} -``` - -### Reverse - -Returns a stream whose elements are reverse order of given stream. Support chainable operation
- -Signature: - -```go -func (s stream[T]) Reverse() stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - reverse := original.Reverse() - - fmt.Println(reverse.ToSlice()) - - // Output: - // [3 2 1] -} -``` - -### Range - -Returns a stream whose elements are in the range from start(included) to end(excluded) original stream.Support chainable operation
- -Signature: - -```go -func (s stream[T]) Range(start, end int) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - s1 := original.Range(0, 0) - s2 := original.Range(0, 1) - s3 := original.Range(0, 3) - s4 := original.Range(1, 2) - - fmt.Println(s1.ToSlice()) - fmt.Println(s2.ToSlice()) - fmt.Println(s3.ToSlice()) - fmt.Println(s4.ToSlice()) - - // Output: - // [] - // [1] - // [1 2 3] - // [2] -} -``` - -### Sorted - -Returns a stream consisting of the elements of this stream, sorted according to the provided less function.Support chainable operation
- -Signature: - -```go -func (s stream[T]) Sorted(less func(a, b T) bool) stream[T] -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{4, 2, 1, 3}) - - sorted := original.Sorted(func(a, b int) bool { return a < b }) - - fmt.Println(original.ToSlice()) - fmt.Println(sorted.ToSlice()) - - // Output: - // [4 2 1 3] - // [1 2 3 4] -} -``` - -### ForEach - -Performs an action for each element of this stream.
- -Signature: - -```go -func (s stream[T]) ForEach(action func(item T)) -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result := 0 - original.ForEach(func(item int) { - result += item - }) - - fmt.Println(result) - - // Output: - // 6 -} -``` - -### Reduce - -Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
- -Signature: - -```go -func (s stream[T]) Reduce(initial T, accumulator func(a, b T) T) T -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result := original.Reduce(0, func(a, b int) int { - return a + b - }) - - fmt.Println(result) - - // Output: - // 6 -} -``` - -### FindFirst - -Returns the first element of this stream and true, or zero value and false if the stream is empty.
- -Signature: - -```go -func (s stream[T]) FindFirst() (T, bool) -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result, ok := original.FindFirst() - - fmt.Println(result) - fmt.Println(ok) - - // Output: - // 1 - // true -} -``` - -### FindLast - -Returns the last element of this stream and true, or zero value and false if the stream is empty.
- -Signature: - -```go -func (s stream[T]) FindLast() (T, bool) -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{3, 2, 1}) - - result, ok := original.FindLast() - - fmt.Println(result) - fmt.Println(ok) - - // Output: - // 1 - // true -} -``` - -### Max - -Returns the maximum element of this stream according to the provided less function. less fuction: a > b
- -Signature: - -```go -func (s stream[T]) Max(less func(a, b T) bool) (T, bool) -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{4, 2, 1, 3}) - - max, ok := original.Max(func(a, b int) bool { return a > b }) - - fmt.Println(max) - fmt.Println(ok) - - // Output: - // 4 - // true -} -``` - -### Min - -Returns the minimum element of this stream according to the provided less function. less fuction: a < b
- -Signature: - -```go -func (s stream[T]) Min(less func(a, b T) bool) (T, bool) -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{4, 2, 1, 3}) - - min, ok := original.Min(func(a, b int) bool { return a < b }) - - fmt.Println(min) - fmt.Println(ok) - - // Output: - // 1 - // true -} -``` - -### AllMatch - -Returns whether all elements of this stream match the provided predicate.
- -Signature: - -```go -func (s stream[T]) AllMatch(predicate func(item T) bool) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result1 := original.AllMatch(func(item int) bool { - return item > 0 - }) - - result2 := original.AllMatch(func(item int) bool { - return item > 1 - }) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### AnyMatch - -Returns whether any elements of this stream match the provided predicate.
- -Signature: - -```go -func (s stream[T]) AnyMatch(predicate func(item T) bool) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result1 := original.AnyMatch(func(item int) bool { - return item > 1 - }) - - result2 := original.AnyMatch(func(item int) bool { - return item > 3 - }) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### NoneMatch - -Returns whether no elements of this stream match the provided predicate.
- -Signature: - -```go -func (s stream[T]) NoneMatch(predicate func(item T) bool) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result1 := original.NoneMatch(func(item int) bool { - return item > 3 - }) - - result2 := original.NoneMatch(func(item int) bool { - return item > 1 - }) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### Count - -Returns the count of elements in the stream.
- -Signature: - -```go -func (s stream[T]) Count() int -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s1 := stream.FromSlice([]int{1, 2, 3}) - s2 := stream.FromSlice([]int{}) - - fmt.Println(s1.Count()) - fmt.Println(s2.Count()) - - // Output: - // 3 - // 0 -} -``` - -### ToSlice - -Returns the elements in the stream.
- -Signature: - -```go -func (s stream[T]) ToSlice() []T -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s := stream.Of(1, 2, 3) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` diff --git a/docs/olddocs/stream_zh-CN.md b/docs/olddocs/stream_zh-CN.md deleted file mode 100644 index 4a2168f..0000000 --- a/docs/olddocs/stream_zh-CN.md +++ /dev/null @@ -1,940 +0,0 @@ -# Stream - -Stream 流,该包仅验证简单 stream 实现,功能有限。 - - - -## 源码: - -- [https://github.com/duke-git/lancet/blob/main/stream/stream.go](https://github.com/duke-git/lancet/blob/main/stream/stream.go) - - - -## 用法: - -```go -import ( - "github.com/duke-git/lancet/v2/stream" -) -``` - - - -## 目录 - -- [Of](#Of) -- [FromSlice](#FromSlice) -- [FromChannel](#FromChannel) -- [FromRange](#FromRange) -- [Generate](#Generate) -- [Concat](#Concat) -- [Distinct](#Distinct) -- [Filter](#Filter) -- [Map](#Map) -- [Peek](#Peek) -- [Skip](#Skip) -- [Limit](#Limit) -- [Reverse](#Reverse) -- [Range](#Range) -- [Sorted](#Sorted) -- [ForEach](#ForEach) -- [Reduce](#Reduce) -- [FindFirst](#FindFirst) -- [FindLast](#FindLast) -- [Max](#Max) -- [Min](#Min) -- [AllMatch](#AllMatch) -- [AnyMatch](#AnyMatch) -- [NoneMatch](#NoneMatch) -- [Count](#Count) -- [ToSlice](#ToSlice) - - - -## 文档 - -### Of - -创建元素为指定值的stream。
- -函数签名: - -```go -func Of[T any](elems ...T) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s := stream.Of(1, 2, 3) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` - -### FromSlice - -从切片创建stream。
- -函数签名: - -```go -func FromSlice[T any](source []T) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s := stream.FromSlice([]int{1, 2, 3}) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` - -### FromChannel - -从通道创建stream。
- -函数签名: - -```go -func FromChannel[T any](source <-chan T) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - ch := make(chan int) - go func() { - for i := 1; i < 4; i++ { - ch <- i - } - close(ch) - }() - - s := stream.FromChannel(ch) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` - -### FromRange - -指定一个范围创建stream, 范围两端点值都包括在内。
- -函数签名: - -```go -func FromRange[T constraints.Integer | constraints.Float](start, end, step T) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s := stream.FromRange(1, 5, 1) - - data := s.ToSlice() - fmt.Println(data) - - // Output: - // [1 2 3 4 5] -} -``` - -### Generate - -创建一个stream,其中每个元素都由提供的生成器函数生成
- -函数签名: - -```go -func Generate[T any](generator func() func() (item T, ok bool)) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - n := 0 - max := 4 - - generator := func() func() (int, bool) { - return func() (int, bool) { - n++ - return n, n < max - } - } - - s := stream.Generate(generator) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` - -### Concat - -创建一个延迟连接stream,其元素是第一个stream的所有元素,后跟第二个stream的全部元素。
- -函数签名: - -```go -func Concat[T any](a, b stream[T]) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s1 := stream.FromSlice([]int{1, 2, 3}) - s2 := stream.FromSlice([]int{4, 5, 6}) - - s := Concat(s1, s2) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### Distinct - -创建并返回一个stream,用于删除重复的项。 支持链式操作
- -函数签名: - -```go -func (s stream[T]) Distinct() stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 2, 3, 3, 3}) - distinct := original.Distinct() - - data1 := original.ToSlice() - data2 := distinct.ToSlice() - - fmt.Println(data1) - fmt.Println(data2) - - // Output: - // [1 2 2 3 3 3] - // [1 2 3] -} -``` - -### Filter - -返回一个通过判定函数的stream 支持链式操作
- -函数签名: - -```go -func (s stream[T]) Filter(predicate func(item T) bool) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3, 4, 5}) - - isEven := func(n int) bool { - return n%2 == 0 - } - - even := original.Filter(isEven) - - fmt.Println(even.ToSlice()) - - // Output: - // [2 4] -} -``` - -### Map - -返回一个stream,该stream由将给定函数应用于源stream元素的元素组成。支持链式操作
- -函数签名: - -```go -func (s stream[T]) Map(mapper func(item T) T) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - addOne := func(n int) int { - return n + 1 - } - - increament := original.Map(addOne) - - fmt.Println(increament.ToSlice()) - - // Output: - // [2 3 4] -} -``` - -### Peek - -返回一个由源stream的元素组成的stream,并在从生成的stream中消耗元素时对每个元素执行所提供的操作。 支持链式操作
- -函数签名: - -```go -func (s stream[T]) Peek(consumer func(item T)) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - data := []string{} - peekStream := original.Peek(func(n int) { - data = append(data, fmt.Sprint("value", n)) - }) - - fmt.Println(original.ToSlice()) - fmt.Println(peekStream.ToSlice()) - fmt.Println(data) - - // Output: - // [1 2 3] - // [1 2 3] - // [value1 value2 value3] -} -``` - -### Skip - -在丢弃stream的前n个元素后,返回由源stream的其余元素组成的stream。如果此stream包含的元素少于n个,则将返回一个空stream。支持链式操作
- -函数签名: - -```go -func (s stream[T]) Skip(n int) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3, 4}) - - s1 := original.Skip(-1) - s2 := original.Skip(0) - s3 := original.Skip(1) - s4 := original.Skip(5) - - fmt.Println(s1.ToSlice()) - fmt.Println(s2.ToSlice()) - fmt.Println(s3.ToSlice()) - fmt.Println(s4.ToSlice()) - - // Output: - // [1 2 3 4] - // [1 2 3 4] - // [2 3 4] - // [] -} -``` - -### Limit - -返回由源stream的元素组成的stream,该stream被截断为长度不超过maxSize。支持链式操作
- -函数签名: - -```go -func (s stream[T]) Limit(maxSize int) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3, 4}) - - s1 := original.Limit(-1) - s2 := original.Limit(0) - s3 := original.Limit(1) - s4 := original.Limit(5) - - fmt.Println(s1.ToSlice()) - fmt.Println(s2.ToSlice()) - fmt.Println(s3.ToSlice()) - fmt.Println(s4.ToSlice()) - - // Output: - // [] - // [] - // [1] - // [1 2 3 4] -} -``` - -### Reverse - -返回元素与源stream的顺序相反的stream。支持链式操作
- -函数签名: - -```go -func (s stream[T]) Reverse() stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - reverse := original.Reverse() - - fmt.Println(reverse.ToSlice()) - - // Output: - // [3 2 1] -} -``` - -### Range - -返回一个stream,该stream的元素在从源stream的开始(包含)到结束(排除)的范围内。支持链式操作
- -函数签名: - -```go -func (s stream[T]) Range(start, end int) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - s1 := original.Range(0, 0) - s2 := original.Range(0, 1) - s3 := original.Range(0, 3) - s4 := original.Range(1, 2) - - fmt.Println(s1.ToSlice()) - fmt.Println(s2.ToSlice()) - fmt.Println(s3.ToSlice()) - fmt.Println(s4.ToSlice()) - - // Output: - // [] - // [1] - // [1 2 3] - // [2] -} -``` - -### Sorted - -返回一个stream,该stream由源stream的元素组成,并根据提供的less函数进行排序。支持链式操作
- -函数签名: - -```go -func (s stream[T]) Sorted(less func(a, b T) bool) stream[T] -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{4, 2, 1, 3}) - - sorted := original.Sorted(func(a, b int) bool { return a < b }) - - fmt.Println(original.ToSlice()) - fmt.Println(sorted.ToSlice()) - - // Output: - // [4 2 1 3] - // [1 2 3 4] -} -``` - -### ForEach - -对stream的每个元素执行一个操作。
- -函数签名: - -```go -func (s stream[T]) ForEach(action func(item T)) -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result := 0 - original.ForEach(func(item int) { - result += item - }) - - fmt.Println(result) - - // Output: - // 6 -} -``` - -### Reduce - -使用关联累加函数对stream的元素执行reduce操作,并reduce操作结果(如果有)。
- -函数签名: - -```go -func (s stream[T]) Reduce(initial T, accumulator func(a, b T) T) T -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result := original.Reduce(0, func(a, b int) int { - return a + b - }) - - fmt.Println(result) - - // Output: - // 6 -} -``` - -### FindFirst - -返回此stream的第一个元素和true,如果stream为空,则返回零值和false。
- -函数签名: - -```go -func (s stream[T]) FindFirst() (T, bool) -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result, ok := original.FindFirst() - - fmt.Println(result) - fmt.Println(ok) - - // Output: - // 1 - // true -} -``` - -### FindLast - -返回此stream最后一个元素和true,如果stream为空,则返回零值和false。
- -函数签名: - -```go -func (s stream[T]) FindLast() (T, bool) -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{3, 2, 1}) - - result, ok := original.FindLast() - - fmt.Println(result) - fmt.Println(ok) - - // Output: - // 1 - // true -} -``` - -### Max - -根据提供的less函数返回stream的最大元素。less 函数: a > b
- -函数签名: - -```go -func (s stream[T]) Max(less func(a, b T) bool) (T, bool) -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{4, 2, 1, 3}) - - max, ok := original.Max(func(a, b int) bool { return a > b }) - - fmt.Println(max) - fmt.Println(ok) - - // Output: - // 4 - // true -} -``` - -### Min - -根据提供的less函数返回stream的最小元素。less函数: a < b
- -函数签名: - -```go -func (s stream[T]) Min(less func(a, b T) bool) (T, bool) -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{4, 2, 1, 3}) - - min, ok := original.Min(func(a, b int) bool { return a < b }) - - fmt.Println(min) - fmt.Println(ok) - - // Output: - // 1 - // true -} -``` - -### AllMatch - -判断stream的所有元素是否全部匹配指定判定函数。
- -函数签名: - -```go -func (s stream[T]) AllMatch(predicate func(item T) bool) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result1 := original.AllMatch(func(item int) bool { - return item > 0 - }) - - result2 := original.AllMatch(func(item int) bool { - return item > 1 - }) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### AnyMatch - -判断stream是否包含匹配指定判定函数的元素。
- -函数签名: - -```go -func (s stream[T]) AnyMatch(predicate func(item T) bool) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result1 := original.AnyMatch(func(item int) bool { - return item > 1 - }) - - result2 := original.AnyMatch(func(item int) bool { - return item > 3 - }) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### NoneMatch - -判断stream的元素是否全部不匹配指定的判定函数。
- -函数签名: - -```go -func (s stream[T]) NoneMatch(predicate func(item T) bool) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - original := stream.FromSlice([]int{1, 2, 3}) - - result1 := original.NoneMatch(func(item int) bool { - return item > 3 - }) - - result2 := original.NoneMatch(func(item int) bool { - return item > 1 - }) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### Count - -返回stream中元素的数量。
- -函数签名: - -```go -func (s stream[T]) Count() int -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s1 := stream.FromSlice([]int{1, 2, 3}) - s2 := stream.FromSlice([]int{}) - - fmt.Println(s1.Count()) - fmt.Println(s2.Count()) - - // Output: - // 3 - // 0 -} -``` - -### ToSlice - -返回stream中的元素切片。
- -函数签名: - -```go -func (s stream[T]) ToSlice() []T -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/stream" -) - -func main() { - s := stream.Of(1, 2, 3) - - data := s.ToSlice() - - fmt.Println(data) - - // Output: - // [1 2 3] -} -``` diff --git a/docs/olddocs/structs/field.md b/docs/olddocs/structs/field.md deleted file mode 100644 index 03d2ceb..0000000 --- a/docs/olddocs/structs/field.md +++ /dev/null @@ -1,354 +0,0 @@ -# Field - -Field is abstract struct field for provide several high level functions - - - -## Source: - -- [https://github.com/duke-git/lancet/blob/main/structs/field.go](https://github.com/duke-git/lancet/blob/main/structs/field.go) - - - -## Usage: - -```go -import ( - "github.com/duke-git/lancet/v2/structs" -) -``` - - - -## Index: - -- [Tag](#Tag) -- [Name](#Name) -- [Value](#Value) -- [Kind](#Kind) -- [IsEmbedded](#IsEmbedded) -- [IsExported](#IsExported) -- [IsZero](#IsZero) -- [IsSlice](#IsSlice) - -> Note:Since `Field` inherits from `Struct`, it also has all the methods of 'Struct', as follows: - -- [ToMap](https://github.com/duke-git/lancet/blob/main/docs/structs/struct.md#ToMap) -- [Fields](https://github.com/duke-git/lancet/blob/main/docs/structs/struct.md#Fields) -- [Field](https://github.com/duke-git/lancet/blob/main/docs/structs/struct.md#Field) -- [IsStruct](https://github.com/duke-git/lancet/blob/main/docs/structs/struct.md#IsStruct) - - - -## Documentation: - -### Tag - -Get a `Tag` of the `Field`, `Tag` is a abstract struct field tag
- -Signature: - -```go -func (f *Field) Tag() *Tag -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string `json:"name,omitempty"` - } - p1 := &Parent{"111"} - - s := structs.New(p1) - n, _ := s.Field("Name") - tag := n.Tag() - - fmt.Println(tag.Name) - - // Output: - // name -} -``` - -### Value - -Get the `Field` underlying value
- -Signature: - -```go -func (f *Field) Value() any -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string `json:"name,omitempty"` - } - p1 := &Parent{"111"} - - s := structs.New(p1) - n, _ := s.Field("Name") - - fmt.Println(n.Value()) - - // Output: - // 111 -} -``` - -### IsEmbedded - -Check if the field is an embedded field
- -Signature: - -```go -func (f *Field) IsEmbedded() bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - } - type Child struct { - Parent - Age int - } - c1 := &Child{} - c1.Name = "111" - c1.Age = 11 - - s := structs.New(c1) - n, _ := s.Field("Name") - a, _ := s.Field("Age") - - fmt.Println(n.IsEmbedded()) - fmt.Println(a.IsEmbedded()) - - // Output: - // true - // false -} -``` - -### IsExported - -Check if the field is exported
- -Signature: - -```go -func (f *Field) IsExported() bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - age int - } - p1 := &Parent{Name: "11", age: 11} - s := structs.New(p1) - n, _ := s.Field("Name") - a, _ := s.Field("age") - - fmt.Println(n.IsExported()) - fmt.Println(a.IsExported()) - - // Output: - // true - // false -} -``` - -### IsZero - -Check if the field is zero value
- -Signature: - -```go -func (f *Field) IsZero() bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - Age int - } - p1 := &Parent{Age: 11} - s := structs.New(p1) - n, _ := s.Field("Name") - a, _ := s.Field("Age") - - fmt.Println(n.IsZero()) - fmt.Println(a.IsZero()) - - // Output: - // true - // false -} -``` - -### Name - -Get the field name
- -Signature: - -```go -func (f *Field) Name() string -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - Age int - } - p1 := &Parent{Age: 11} - s := structs.New(p1) - n, _ := s.Field("Name") - a, _ := s.Field("Age") - - fmt.Println(n.Name()) - fmt.Println(a.Name()) - - // Output: - // Name - // Age -} -``` - -### Kind - -Get the field's kind
- -Signature: - -```go -func (f *Field) Kind() reflect.Kind -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - Age int - } - p1 := &Parent{Age: 11} - s := structs.New(p1) - n, _ := s.Field("Name") - a, _ := s.Field("Age") - - fmt.Println(n.Kind()) - fmt.Println(a.Kind()) - - // Output: - // string - // int -} -``` - -### IsSlice - -Check if the field is a slice
- -Signature: - -```go -func (f *Field) IsSlice() bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - arr []int - } - - p1 := &Parent{arr: []int{1, 2, 3}} - s := structs.New(p1) - a, _ := s.Field("arr") - - fmt.Println(a.IsSlice()) - - // Output: - // true -} -``` diff --git a/docs/olddocs/structs/field_zh-CN.md b/docs/olddocs/structs/field_zh-CN.md deleted file mode 100644 index 03e455c..0000000 --- a/docs/olddocs/structs/field_zh-CN.md +++ /dev/null @@ -1,353 +0,0 @@ -# Field - -Field 包封装了一个抽象的`Field`结构体,提供了操作`struct`属性的相关函数 - - - -## 源码: - -- [https://github.com/duke-git/lancet/blob/main/structs/field.go](https://github.com/duke-git/lancet/blob/main/structs/field.go) - - - -## 用法: - -```go -import ( - "github.com/duke-git/lancet/v2/structs" -) -``` - - - -## 目录: -- [Tag](#Tag) -- [Name](#Name) -- [Value](#Value) -- [Kind](#Kind) -- [IsEmbedded](#IsEmbedded) -- [IsExported](#IsExported) -- [IsZero](#IsZero) -- [IsSlice](#IsSlice) - -> 注意:由于`Field`继承于`Struct`,所以同样拥有`Struct`所有方法,如下: - -- [ToMap](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#ToMap) -- [Fields](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#Fields) -- [Field](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#Field) -- [IsStruct](https://github.com/duke-git/lancet/blob/main/docs/structs/struct_zh-CN.md#IsStruct) - - - -## API 文档: - -### Tag - -获取`Field`的`Tag`,默认的tag key是json
- -函数签名: - -```go -func (f *Field) Tag() *Tag -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string `json:"name,omitempty"` - } - p1 := &Parent{"111"} - - s := structs.New(p1) - n, _ := s.Field("Name") - tag := n.Tag() - - fmt.Println(tag.Name) - - // Output: - // name -} -``` - -### Value - -获取`Field`属性的值
- -函数签名: - -```go -func (f *Field) Value() any -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string `json:"name,omitempty"` - } - p1 := &Parent{"111"} - - s := structs.New(p1) - n, _ := s.Field("Name") - - fmt.Println(n.Value()) - - // Output: - // 111 -} -``` - -### IsEmbedded - -判断属性是否为嵌入
- -函数签名: - -```go -func (f *Field) IsEmbedded() bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - } - type Child struct { - Parent - Age int - } - c1 := &Child{} - c1.Name = "111" - c1.Age = 11 - - s := structs.New(c1) - n, _ := s.Field("Name") - a, _ := s.Field("Age") - - fmt.Println(n.IsEmbedded()) - fmt.Println(a.IsEmbedded()) - - // Output: - // true - // false -} -``` - -### IsExported - -判断属性是否导出
- -函数签名: - -```go -func (f *Field) IsExported() bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - age int - } - p1 := &Parent{Name: "11", age: 11} - s := structs.New(p1) - n, _ := s.Field("Name") - a, _ := s.Field("age") - - fmt.Println(n.IsExported()) - fmt.Println(a.IsExported()) - - // Output: - // true - // false -} -``` - -### IsZero - -判断属性是否为零值
- -函数签名: - -```go -func (f *Field) IsZero() bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - Age int - } - p1 := &Parent{Age: 11} - s := structs.New(p1) - n, _ := s.Field("Name") - a, _ := s.Field("Age") - - fmt.Println(n.IsZero()) - fmt.Println(a.IsZero()) - - // Output: - // true - // false -} -``` - -### Name - -获取属性名
- -函数签名: - -```go -func (f *Field) Name() string -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - Age int - } - p1 := &Parent{Age: 11} - s := structs.New(p1) - n, _ := s.Field("Name") - a, _ := s.Field("Age") - - fmt.Println(n.Name()) - fmt.Println(a.Name()) - - // Output: - // Name - // Age -} -``` - -### Kind - -获取属性Kind
- -函数签名: - -```go -func (f *Field) Kind() reflect.Kind -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - Age int - } - p1 := &Parent{Age: 11} - s := structs.New(p1) - n, _ := s.Field("Name") - a, _ := s.Field("Age") - - fmt.Println(n.Kind()) - fmt.Println(a.Kind()) - - // Output: - // string - // int -} -``` - -### IsSlice - -判断属性是否是切片
- -函数签名: - -```go -func (f *Field) IsSlice() bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type Parent struct { - Name string - arr []int - } - - p1 := &Parent{arr: []int{1, 2, 3}} - s := structs.New(p1) - a, _ := s.Field("arr") - - fmt.Println(a.IsSlice()) - - // Output: - // true -} -``` \ No newline at end of file diff --git a/docs/olddocs/structs/struct.md b/docs/olddocs/structs/struct.md deleted file mode 100644 index e229f46..0000000 --- a/docs/olddocs/structs/struct.md +++ /dev/null @@ -1,214 +0,0 @@ -# Structs - -Struct is abstract struct for provide several high level functions - - - -## Source: - -- [https://github.com/duke-git/lancet/blob/main/structs/struct.go](https://github.com/duke-git/lancet/blob/main/structs/struct.go) - - - -## Usage: - -```go -import ( - "github.com/duke-git/lancet/v2/structs" -) -``` - - - -## Index: - -- [New](#New) -- [ToMap](#ToMap) -- [Fields](#Fields) -- [Field](#Field) -- [IsStruct](#IsStruct) - - - -## Documentation: - -### New - -The constructor function of the `Struct`
- -Signature: - -```go -func New(value any, tagName ...string) *Struct -``` - -Example: - -```go -package main - -import ( - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s := structs.New(p1) - // to do something -} -``` - -### ToMap - -convert a valid struct to a map
- -Signature: - -```go -func (s *Struct) ToMap() (map[string]any, error) -``` - -> In addition, provided a convenient static function ToMap - -```go -func ToMap(v any) (map[string]any, error) -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - // use constructor function - s1 := structs.New(p1) - m1, _ := s1.ToMap() - - fmt.Println(m1) - - // use static function - m2, _ := structs.ToMap(p1) - - fmt.Println(m2) - - // Output: - // map[name:11] - // map[name:11] -} -``` - -### Fields - -Get all fields of a given struct, that the fields are abstract struct field
- -Signature: - -```go -func (s *Struct) Fields() []*Field -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s := structs.New(p1) - fields := s.Fields() - - fmt.Println(len(fields)) - - // Output: - // 1 -} -``` - -### Field - -Get an abstract field of a struct by given field name
- -Signature: - -```go -func (s *Struct) Field(name string) *Field -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s := structs.New(p1) - f := s.Field("Name") - - fmt.Println(f.Value()) - - // Output: - // 11 -} -``` - -### IsStruct - -Check if the struct is valid
- -Signature: - -```go -func (s *Struct) IsStruct() bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s := structs.New(p1) - - fmt.Println(s.IsStruct()) - - // Output: - // true -} -``` diff --git a/docs/olddocs/structs/struct_zh-CN.md b/docs/olddocs/structs/struct_zh-CN.md deleted file mode 100644 index e4fc0bf..0000000 --- a/docs/olddocs/structs/struct_zh-CN.md +++ /dev/null @@ -1,213 +0,0 @@ -# Structs - -structs 包封装了一个抽象的`Struct`结构体,提供了操作`struct`的相关函数 - - - -## 源码: - -- [https://github.com/duke-git/lancet/blob/main/structs/struct.go](https://github.com/duke-git/lancet/blob/main/structs/struct.go) - - - -## 用法: - -```go -import ( - "github.com/duke-git/lancet/v2/structs" -) -``` - - - -## 目录: - -- [New](#New) -- [ToMap](#ToMap) -- [Fields](#Fields) -- [Field](#Field) -- [IsStruct](#IsStruct) - - - -## API 文档: - -### New - -`Struct`结构体的构造函数
- -函数签名: - -```go -func New(value any, tagName ...string) *Struct -``` - -示例: - -```go -package main - -import ( - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s := structs.New(p1) - // to do something -} -``` - -### ToMap - -将一个合法的struct对象转换为map[string]any
- -函数签名: - -```go -func (s *Struct) ToMap() (map[string]any, error) -``` - -除此之外,提供一个便捷的静态方法 ToMap - -```go -func ToMap(v any) (map[string]any, error) -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s1 := structs.New(p1) - m1, _ := s1.ToMap() - - fmt.Println(m1) - - // 如果不需要Struct更多的方法,可以直接使用ToMap - m2, _ := structs.ToMap(p1) - - fmt.Println(m2) - - // Output: - // map[name:11] - // map[name:11] -} -``` - -### Fields - -获取一个struct对象的属性列表
- -函数签名: - -```go -func (s *Struct) Fields() []*Field -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s := structs.New(p1) - fields := s.Fields() - - fmt.Println(len(fields)) - - // Output: - // 1 -} -``` - -### Field - -根据属性名获取一个struct对象的属性
- -函数签名: - -```go -func (s *Struct) Field(name string) *Field -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s := structs.New(p1) - f := s.Field("Name") - - fmt.Println(f.Value()) - - // Output: - // 11 -} -``` - -### IsStruct - -判断是否为一个合法的struct对象
- -函数签名: - -```go -func (s *Struct) IsStruct() bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/structs" -) - -func main() { - type People struct { - Name string `json:"name"` - } - p1 := &People{Name: "11"} - s := structs.New(p1) - - fmt.Println(s.IsStruct()) - - // Output: - // true -} -``` diff --git a/docs/olddocs/strutil.md b/docs/olddocs/strutil.md deleted file mode 100644 index 20d5627..0000000 --- a/docs/olddocs/strutil.md +++ /dev/null @@ -1,1425 +0,0 @@ -# Strutil - -Package strutil contains some functions to manipulate string. - - - -## Source: - -- [https://github.com/duke-git/lancet/blob/main/strutil/string.go](https://github.com/duke-git/lancet/blob/main/strutil/string.go) - - - -## Usage: - -```go -import ( - "github.com/duke-git/lancet/v2/strutil" -) -``` - - - -## Index - -- [After](#After) -- [AfterLast](#AfterLast) -- [Before](#Before) -- [BeforeLast](#BeforeLast) -- [CamelCase](#CamelCase) -- [Capitalize](#Capitalize) -- [IsString](#IsString) -- [KebabCase](#KebabCase) -- [UpperKebabCase](#UpperKebabCase) -- [LowerFirst](#LowerFirst) -- [UpperFirst](#UpperFirst) -- [Pad](#Pad) -- [PadStart](#PadStart) -- [PadEnd](#PadEnd) -- [Reverse](#Reverse) -- [SnakeCase](#SnakeCase) -- [UpperSnakeCase](#UpperSnakeCase) -- [SplitEx](#SplitEx) -- [Substring](#Substring) -- [Wrap](#Wrap) -- [Unwrap](#Unwrap) -- [SplitWords](#SplitWords) -- [WordCount](#WordCount) -- [RemoveNonPrintable](#RemoveNonPrintable) -- [StringToBytes](#StringToBytes) -- [BytesToString](#BytesToString) -- [IsBlank](#IsBlank) -- [HasPrefixAny](#HasPrefixAny) -- [HasSuffixAny](#HasSuffixAny) -- [IndexOffset](#IndexOffset) -- [ReplaceWithMap](#ReplaceWithMap) -- [Trim](#Trim) -- [SplitAndTrim](#SplitAndTrim) -- [HideString](#HideString) -- [ContainsAll](#ContainsAll) -- [ContainsAny](#ContainsAny) -- [RemoveWhiteSpace](#RemoveWhiteSpace) - - - -## Documentation - -### After - -Returns the substring after the first occurrence of a specified string in the source string.
- -Signature: - -```go -func After(s, char string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.After("foo", "") - result2 := strutil.After("foo", "foo") - result3 := strutil.After("foo/bar", "foo") - result4 := strutil.After("foo/bar", "/") - result5 := strutil.After("foo/bar/baz", "/") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // foo - // - // /bar - // bar - // bar/baz -} -``` - -### AfterLast - -Returns the substring after the last occurrence of a specified string in the source string.
- -Signature: - -```go -func AfterLast(s, char string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.AfterLast("foo", "") - result2 := strutil.AfterLast("foo", "foo") - result3 := strutil.AfterLast("foo/bar", "/") - result4 := strutil.AfterLast("foo/bar/baz", "/") - result5 := strutil.AfterLast("foo/bar/foo/baz", "foo") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // foo - // - // bar - // baz - // /baz -} -``` - -### Before - -Returns the substring of the source string up to the first occurrence of the specified string.
- -Signature: - -```go -func Before(s, char string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Before("foo", "") - result2 := strutil.Before("foo", "foo") - result3 := strutil.Before("foo/bar", "/") - result4 := strutil.Before("foo/bar/baz", "/") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // foo - // - // foo - // foo -} -``` - -### BeforeLast - -Returns the substring of the source string up to the last occurrence of the specified string.
- -Signature: - -```go -func BeforeLast(s, char string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.BeforeLast("foo", "") - result2 := strutil.BeforeLast("foo", "foo") - result3 := strutil.BeforeLast("foo/bar", "/") - result4 := strutil.BeforeLast("foo/bar/baz", "/") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // foo - // - // foo - // foo/bar -} -``` - -### CamelCase - -Coverts string to camelCase string, non letters and numbers will be ignored.
- -Signature: - -```go -func CamelCase(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.CamelCase(v) - fmt.Println(s) - } - - // Output: - // - // foobar - // fooBarBaz - // foo - // foo11Bar -} -``` - -### KebabCase - -KebabCase covert string to kebab-case, non letters and numbers will be ignored.
- -Signature: - -```go -func KebabCase(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.KebabCase(v) - fmt.Println(s) - } - - // Output: - // - // foo-bar - // foo-bar - // foobar - // foo-1-1-bar -} -``` - -### UpperKebabCase - -UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.
- -Signature: - -```go -func UpperKebabCase(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.UpperKebabCase(v) - fmt.Println(s) - } - - // Output: - // - // FOO-BAR - // FOO-BAR - // FOO-BAR - // FOO-1-1-BAR -} -``` - -### Capitalize - -Convert the first character of a string to upper case.
- -Signature: - -```go -func Capitalize(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"} - - for _, v := range strings { - s := strutil.Capitalize(v) - fmt.Println(s) - } - - // Output: - // - // Foo - // _foo - // Foobar - // Foo-bar -} -``` - -### IsString - -Check if the value's data type is string.
- -Signature: - -```go -func IsString(v any) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.IsString("") - result2 := strutil.IsString("a") - result3 := strutil.IsString(1) - result4 := strutil.IsString(true) - result5 := strutil.IsString([]string{"a"}) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // true - // true - // false - // false - // false -} -``` - -### LowerFirst - -Convert the first character of string to lower case.
- -Signature: - -```go -func LowerFirst(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "bar", "BAr", "Bar大"} - - for _, v := range strings { - s := strutil.LowerFirst(v) - fmt.Println(s) - } - - // Output: - // - // bar - // bAr - // bar大 -} -``` - -### UpperFirst - -Convert the first character of string to upper case.
- -Signature: - -```go -func UpperFirst(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "bar", "BAr", "bar大"} - - for _, v := range strings { - s := strutil.UpperFirst(v) - fmt.Println(s) - } - - // Output: - // - // Bar - // BAr - // Bar大 -} -``` - -### Pad - -Pads string on the left and right side if it's shorter than size.
- -Signature: - -```go -func Pad(source string, size int, padStr string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Pad("foo", 1, "bar") - result2 := strutil.Pad("foo", 2, "bar") - result3 := strutil.Pad("foo", 3, "bar") - result4 := strutil.Pad("foo", 4, "bar") - result5 := strutil.Pad("foo", 5, "bar") - result6 := strutil.Pad("foo", 6, "bar") - result7 := strutil.Pad("foo", 7, "bar") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - // Output: - // foo - // foo - // foo - // foob - // bfoob - // bfooba - // bafooba -} -``` - -### PadEnd - -Pads string on the right side if it's shorter than size.
- -Signature: - -```go -func PadEnd(source string, size int, padStr string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.PadEnd("foo", 1, "bar") - result2 := strutil.PadEnd("foo", 2, "bar") - result3 := strutil.PadEnd("foo", 3, "bar") - result4 := strutil.PadEnd("foo", 4, "bar") - result5 := strutil.PadEnd("foo", 5, "bar") - result6 := strutil.PadEnd("foo", 6, "bar") - result7 := strutil.PadEnd("foo", 7, "bar") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // foo - // foo - // foo - // foob - // fooba - // foobar - // foobarb -} -``` - -### PadStart - -Pads string on the left side if it's shorter than size.
- -Signature: - -```go -func PadStart(source string, size int, padStr string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.PadStart("foo", 1, "bar") - result2 := strutil.PadStart("foo", 2, "bar") - result3 := strutil.PadStart("foo", 3, "bar") - result4 := strutil.PadStart("foo", 4, "bar") - result5 := strutil.PadStart("foo", 5, "bar") - result6 := strutil.PadStart("foo", 6, "bar") - result7 := strutil.PadStart("foo", 7, "bar") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // foo - // foo - // foo - // bfoo - // bafoo - // barfoo - // barbfoo -} -``` - -### Reverse - -Return string whose char order is reversed to the given string.
- -Signature: - -```go -func Reverse(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - s := "foo" - rs := strutil.Reverse(s) - - fmt.Println(s) - fmt.Println(rs) - - // Output: - // foo - // oof -} -``` - -### SnakeCase - -Coverts string to snake_case, non letters and numbers will be ignored.
- -Signature: - -```go -func SnakeCase(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.SnakeCase(v) - fmt.Println(s) - } - - // Output: - // - // foo_bar - // foo_bar - // foobar - // foo_1_1_bar -} -``` - -### UpperSnakeCase - -Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.
- -Signature: - -```go -func SnakeCase(s string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.UpperSnakeCase(v) - fmt.Println(s) - } - - // Output: - // - // FOO_BAR - // FOO_BAR - // FOO_BAR - // FOO_1_1_BAR -} -``` - -### SplitEx - -Split a given string whether the result contains empty string.
- -Signature: - -```go -func SplitEx(s, sep string, removeEmptyString bool) []string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.SplitEx(" a b c ", "", true) - - result2 := strutil.SplitEx(" a b c ", " ", false) - result3 := strutil.SplitEx(" a b c ", " ", true) - - result4 := strutil.SplitEx("a = b = c = ", " = ", false) - result5 := strutil.SplitEx("a = b = c = ", " = ", true) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // [] - // [ a b c ] - // [a b c] - // [a b c ] -} -``` - -### Substring - -Returns a substring of the specified length starting at the specified offset position.
- -Signature: - -```go -func Substring(s string, offset int, length uint) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Substring("abcde", 1, 3) - result2 := strutil.Substring("abcde", 1, 5) - result3 := strutil.Substring("abcde", -1, 3) - result4 := strutil.Substring("abcde", -2, 2) - result5 := strutil.Substring("abcde", -2, 3) - result6 := strutil.Substring("你好,欢迎你", 0, 2) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // bcd - // bcde - // e - // de - // de - // 你好 -} -``` - -### Wrap - -Wrap a string with given string.
- -Signature: - -```go -func Wrap(str string, wrapWith string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Wrap("foo", "") - result2 := strutil.Wrap("foo", "*") - result3 := strutil.Wrap("'foo'", "'") - result4 := strutil.Wrap("", "*") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // foo - // *foo* - // ''foo'' - // -} -``` - -### Wrap - -Unwrap a given string from anther string. will change source string.
- -Signature: - -```go -func Unwrap(str string, wrapToken string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Unwrap("foo", "") - result2 := strutil.Unwrap("*foo*", "*") - result3 := strutil.Unwrap("*foo", "*") - result4 := strutil.Unwrap("foo*", "*") - result5 := strutil.Unwrap("**foo**", "*") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // foo - // foo - // *foo - // foo* - // *foo* -} -``` - -### SplitWords - -Splits a string into words, word only contains alphabetic characters.
- -Signature: - -```go -func SplitWords(s string) []string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.SplitWords("a word") - result2 := strutil.SplitWords("I'am a programmer") - result3 := strutil.SplitWords("Bonjour, je suis programmeur") - result4 := strutil.SplitWords("a -b-c' 'd'e") - result5 := strutil.SplitWords("你好,我是一名码农") - result6 := strutil.SplitWords("こんにちは,私はプログラマーです") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // [a word] - // [I'am a programmer] - // [Bonjour je suis programmeur] - // [a b-c' d'e] - // [] - // [] -} -``` - -### WordCount - -Return the number of meaningful word, word only contains alphabetic characters.
- -Signature: - -```go -func WordCount(s string) int -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.WordCount("a word") - result2 := strutil.WordCount("I'am a programmer") - result3 := strutil.WordCount("Bonjour, je suis programmeur") - result4 := strutil.WordCount("a -b-c' 'd'e") - result5 := strutil.WordCount("你好,我是一名码农") - result6 := strutil.WordCount("こんにちは,私はプログラマーです") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // 2 - // 3 - // 4 - // 3 - // 0 - // 0 -} -``` - -### RemoveNonPrintable - -Remove non-printable characters from a string.
- -Signature: - -```go -func RemoveNonPrintable(str string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n") - result2 := strutil.RemoveNonPrintable("你好😄") - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // hello world - // 你好😄 -} -``` - -### StringToBytes - -Converts a string to byte slice without a memory allocation.
- -Signature: - -```go -func StringToBytes(str string) (b []byte) -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.StringToBytes("abc") - result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'}) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // [97 98 99] - // true -} -``` - -### BytesToString - -Converts a byte slice to string without a memory allocation.
- -Signature: - -```go -func BytesToString(bytes []byte) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - bytes := []byte{'a', 'b', 'c'} - result := strutil.BytesToString(bytes) - - fmt.Println(result) - - // Output: - // abc -} -``` - -### IsBlank - -Checks if a string is whitespace or empty.
- -Signature: - -```go -func IsBlank(str string) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.IsBlank("") - result2 := strutil.IsBlank("\t\v\f\n") - result3 := strutil.IsBlank(" 中文") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - - // Output: - // true - // true - // false -} -``` - -### HasPrefixAny - -Checks if a string starts with any of an array of specified strings.
- -Signature: - -```go -func ReplaceWithMap(str string, replaces map[string]string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"}) - result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"}) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### HasSuffixAny - -Checks if a string ends with any of an array of specified strings.
- -Signature: - -```go -func HasSuffixAny(str string, suffixes []string) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"}) - result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"}) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### IndexOffset - -Returns the index of the first instance of substr in string after offsetting the string by `idxFrom`, or -1 if substr is not present in string.
- -Signature: - -```go -func IndexOffset(str string, substr string, idxFrom int) int -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "foo bar hello world" - - result1 := strutil.IndexOffset(str, "o", 5) - result2 := strutil.IndexOffset(str, "o", 0) - result3 := strutil.IndexOffset(str, "d", len(str)-1) - result4 := strutil.IndexOffset(str, "d", len(str)) - result5 := strutil.IndexOffset(str, "f", -1) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // 12 - // 1 - // 18 - // -1 - // -1 -} -``` - -### ReplaceWithMap - -Returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.
- -Signature: - -```go -func ReplaceWithMap(str string, replaces map[string]string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "ac ab ab ac" - replaces := map[string]string{ - "a": "1", - "b": "2", - } - - result := strutil.ReplaceWithMap(str, replaces) - - fmt.Println(result) - // Output: - // 1c 12 12 1c -} -``` - -### Trim - -Strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.
- -Signature: - -```go -func Trim(str string, characterMask ...string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Trim("\nabcd") - - str := "$ ab cd $ " - - result2 := strutil.Trim(str) - result3 := strutil.Trim(str, "$") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - - // Output: - // abcd - // $ ab cd $ - // ab cd -} -``` - -### SplitAndTrim - -Splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of slice. It ignores the elements which are empty after Trim.
- -Signature: - -```go -func SplitAndTrim(str, delimiter string, characterMask ...string) []string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := " a,b, c,d,$1 " - - result1 := strutil.SplitAndTrim(str, ",") - result2 := strutil.SplitAndTrim(str, ",", "$") - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // [a b c d $1] - // [a b c d 1] -} -``` - -### HideString - -Hide some chars in source string with param `replaceChar`. replace range is origin[start : end]. [start, end).
- -Signature: - -```go -func HideString(origin string, start, end int, replaceChar string) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "13242658976" - - result1 := strutil.HideString(str, 3, 3, "*") - result2 := strutil.HideString(str, 3, 4, "*") - result3 := strutil.HideString(str, 3, 7, "*") - result4 := strutil.HideString(str, 7, 11, "*") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // 13242658976 - // 132*2658976 - // 132****8976 - // 1324265**** -} -``` - -### ContainsAll - -Return true if target string contains all the substrings.
- -Signature: - -```go -func ContainsAll(str string, substrs []string) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "hello world" - - result1 := strutil.ContainsAll(str, []string{"hello", "world"}) - result2 := strutil.ContainsAll(str, []string{"hello", "abc"}) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### ContainsAny - -Return true if target string contains any one of the substrings.
- -Signature: - -```go -func ContainsAny(str string, substrs []string) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "hello world" - - result1 := strutil.ContainsAny(str, []string{"hello", "world"}) - result2 := strutil.ContainsAny(str, []string{"hello", "abc"}) - result3 := strutil.ContainsAny(str, []string{"123", "abc"}) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - - // Output: - // true - // true - // false -} -``` - -### RemoveWhiteSpace - -Remove whitespace characters from a string. when set repalceAll is true removes all whitespace, false only replaces consecutive whitespace characters with one space.
- -Signature: - -```go -func RemoveWhiteSpace(str string, repalceAll bool) string -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := " hello \r\n \t world" - - result1 := strutil.RemoveWhiteSpace(str, true) - result2 := strutil.RemoveWhiteSpace(str, false) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // helloworld - // hello world -} -``` diff --git a/docs/olddocs/strutil_zh-CN.md b/docs/olddocs/strutil_zh-CN.md deleted file mode 100644 index 309a471..0000000 --- a/docs/olddocs/strutil_zh-CN.md +++ /dev/null @@ -1,1424 +0,0 @@ -# Strutil - -strutil 包含处理字符串的相关函数。 - - - -## 源码: - -- [https://github.com/duke-git/lancet/blob/main/strutil/string.go](https://github.com/duke-git/lancet/blob/main/strutil/string.go) - - - -## 用法: - -```go -import ( - "github.com/duke-git/lancet/v2/strutil" -) -``` - - - -## 目录 - -- [After](#After) -- [AfterLast](#AfterLast) -- [Before](#Before) -- [BeforeLast](#BeforeLast) -- [CamelCase](#CamelCase) -- [Capitalize](#Capitalize) -- [IsString](#IsString) -- [KebabCase](#KebabCase) -- [UpperKebabCase](#UpperKebabCase) -- [LowerFirst](#LowerFirst) -- [UpperFirst](#UpperFirst) -- [Pad](#Pad) -- [PadEnd](#PadEnd) -- [PadStart](#PadStart) -- [Reverse](#Reverse) -- [SnakeCase](#SnakeCase) -- [UpperSnakeCase](#UpperSnakeCase) -- [SplitEx](#SplitEx) -- [Substring](#Substring) -- [Wrap](#Wrap) -- [Unwrap](#Unwrap) -- [SplitWords](#SplitWords) -- [WordCount](#WordCount) -- [RemoveNonPrintable](#RemoveNonPrintable) -- [StringToBytes](#StringToBytes) -- [BytesToString](#BytesToString) -- [IsBlank](#IsBlank) -- [HasPrefixAny](#HasPrefixAny) -- [HasSuffixAny](#HasSuffixAny) -- [IndexOffset](#IndexOffset) -- [ReplaceWithMap](#ReplaceWithMap) -- [Trim](#Trim) -- [SplitAndTrim](#SplitAndTrim) -- [HideString](#HideString) -- [ContainsAll](#ContainsAll) -- [ContainsAny](#ContainsAny) -- [RemoveWhiteSpace](#RemoveWhiteSpace) - - - -## Documentation 文档 - -### After - -返回源字符串中特定字符串首次出现时的位置之后的子字符串。
- -函数签名: - -```go -func After(s, char string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.After("foo", "") - result2 := strutil.After("foo", "foo") - result3 := strutil.After("foo/bar", "foo") - result4 := strutil.After("foo/bar", "/") - result5 := strutil.After("foo/bar/baz", "/") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // foo - // - // /bar - // bar - // bar/baz -} -``` - -### AfterLast - -返回源字符串中指定字符串最后一次出现时的位置之后的子字符串。
- -函数签名: - -```go -func AfterLast(s, char string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.AfterLast("foo", "") - result2 := strutil.AfterLast("foo", "foo") - result3 := strutil.AfterLast("foo/bar", "/") - result4 := strutil.AfterLast("foo/bar/baz", "/") - result5 := strutil.AfterLast("foo/bar/foo/baz", "foo") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // foo - // - // bar - // baz - // /baz -} -``` - -### Before - -返回源字符串中指定字符串第一次出现时的位置之前的子字符串。
- -函数签名: - -```go -func Before(s, char string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Before("foo", "") - result2 := strutil.Before("foo", "foo") - result3 := strutil.Before("foo/bar", "/") - result4 := strutil.Before("foo/bar/baz", "/") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // foo - // - // foo - // foo -} -``` - -### BeforeLast - -返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。
- -函数签名: - -```go -func BeforeLast(s, char string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.BeforeLast("foo", "") - result2 := strutil.BeforeLast("foo", "foo") - result3 := strutil.BeforeLast("foo/bar", "/") - result4 := strutil.BeforeLast("foo/bar/baz", "/") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // foo - // - // foo - // foo/bar -} -``` - -### CamelCase - -将字符串转换为驼峰式字符串, 非字母和数字会被忽略。
- -函数签名: - -```go -func CamelCase(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.CamelCase(v) - fmt.Println(s) - } - - // Output: - // - // foobar - // fooBarBaz - // foo - // foo11Bar -} -``` - -### KebabCase - -将字符串转换为kebab-case, 非字母和数字会被忽略。
- -函数签名: - -```go -func KebabCase(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.KebabCase(v) - fmt.Println(s) - } - - // Output: - // - // foo-bar - // foo-bar - // foobar - // foo-1-1-bar -} -``` - -### UpperKebabCase - -将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略。
- -函数签名: - -```go -func UpperKebabCase(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.UpperKebabCase(v) - fmt.Println(s) - } - - // Output: - // - // FOO-BAR - // FOO-BAR - // FOO-BAR - // FOO-1-1-BAR -} -``` - -### Capitalize - -将字符串的第一个字符转换为大写。
- -函数签名: - -```go -func Capitalize(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"} - - for _, v := range strings { - s := strutil.Capitalize(v) - fmt.Println(s) - } - - // Output: - // - // Foo - // _foo - // Foobar - // Foo-bar -} -``` - -### IsString - -判断传入参数的数据类型是否为字符串。
- -函数签名: - -```go -func IsString(v any) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.IsString("") - result2 := strutil.IsString("a") - result3 := strutil.IsString(1) - result4 := strutil.IsString(true) - result5 := strutil.IsString([]string{"a"}) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // true - // true - // false - // false - // false -} -``` - -### LowerFirst - -将字符串的第一个字符转换为小写。
- -函数签名: - -```go -func LowerFirst(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "bar", "BAr", "Bar大"} - - for _, v := range strings { - s := strutil.LowerFirst(v) - fmt.Println(s) - } - - // Output: - // - // bar - // bAr - // bar大 -} -``` - -### UpperFirst - -将字符串的第一个字符转换为大写形式。
- -函数签名: - -```go -func UpperFirst(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "bar", "BAr", "bar大"} - - for _, v := range strings { - s := strutil.UpperFirst(v) - fmt.Println(s) - } - - // Output: - // - // Bar - // BAr - // Bar大 -} -``` - -### Pad - -如果字符串长度短于size,则在左右两侧填充字符串。
- -函数签名: - -```go -func Pad(source string, size int, padStr string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Pad("foo", 1, "bar") - result2 := strutil.Pad("foo", 2, "bar") - result3 := strutil.Pad("foo", 3, "bar") - result4 := strutil.Pad("foo", 4, "bar") - result5 := strutil.Pad("foo", 5, "bar") - result6 := strutil.Pad("foo", 6, "bar") - result7 := strutil.Pad("foo", 7, "bar") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - // Output: - // foo - // foo - // foo - // foob - // bfoob - // bfooba - // bafooba -} -``` - -### PadEnd - -如果字符串长度短于size,则在右侧填充字符串。
- -函数签名: - -```go -func PadEnd(source string, size int, padStr string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.PadEnd("foo", 1, "bar") - result2 := strutil.PadEnd("foo", 2, "bar") - result3 := strutil.PadEnd("foo", 3, "bar") - result4 := strutil.PadEnd("foo", 4, "bar") - result5 := strutil.PadEnd("foo", 5, "bar") - result6 := strutil.PadEnd("foo", 6, "bar") - result7 := strutil.PadEnd("foo", 7, "bar") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // foo - // foo - // foo - // foob - // fooba - // foobar - // foobarb -} -``` - -### PadStart - -如果字符串长度短于size,则在左侧填充字符串。
- -函数签名: - -```go -func PadStart(source string, size int, padStr string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.PadStart("foo", 1, "bar") - result2 := strutil.PadStart("foo", 2, "bar") - result3 := strutil.PadStart("foo", 3, "bar") - result4 := strutil.PadStart("foo", 4, "bar") - result5 := strutil.PadStart("foo", 5, "bar") - result6 := strutil.PadStart("foo", 6, "bar") - result7 := strutil.PadStart("foo", 7, "bar") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // foo - // foo - // foo - // bfoo - // bafoo - // barfoo - // barbfoo -} -``` - -### Reverse - -返回字符顺序与给定字符串相反的字符串。
- -函数签名: - -```go -func Reverse(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - s := "foo" - rs := strutil.Reverse(s) - - fmt.Println(s) - fmt.Println(rs) - - // Output: - // foo - // oof -} -``` - -### SnakeCase - -将字符串转换为snake_case形式, 非字母和数字会被忽略。
- -函数签名: - -```go -func SnakeCase(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.SnakeCase(v) - fmt.Println(s) - } - - // Output: - // - // foo_bar - // foo_bar - // foobar - // foo_1_1_bar -} -``` - -### UpperSnakeCase - -将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略。
- -函数签名: - -```go -func SnakeCase(s string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} - - for _, v := range strings { - s := strutil.UpperSnakeCase(v) - fmt.Println(s) - } - - // Output: - // - // FOO_BAR - // FOO_BAR - // FOO_BAR - // FOO_1_1_BAR -} -``` - -### SplitEx - -分割字符串为切片,removeEmptyString参数指定是否去除空字符串。
- -函数签名: - -```go -func SplitEx(s, sep string, removeEmptyString bool) []string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.SplitEx(" a b c ", "", true) - - result2 := strutil.SplitEx(" a b c ", " ", false) - result3 := strutil.SplitEx(" a b c ", " ", true) - - result4 := strutil.SplitEx("a = b = c = ", " = ", false) - result5 := strutil.SplitEx("a = b = c = ", " = ", true) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // [] - // [ a b c ] - // [a b c] - // [a b c ] -} -``` - -### Substring - -根据指定的位置和长度截取字符串。
- -函数签名: - -```go -func Substring(s string, offset int, length uint) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Substring("abcde", 1, 3) - result2 := strutil.Substring("abcde", 1, 5) - result3 := strutil.Substring("abcde", -1, 3) - result4 := strutil.Substring("abcde", -2, 2) - result5 := strutil.Substring("abcde", -2, 3) - result6 := strutil.Substring("你好,欢迎你", 0, 2) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // bcd - // bcde - // e - // de - // de - // 你好 -} -``` - -### Wrap - -用另一个字符串包裹一个字符串。
- -函数签名: - -```go -func Wrap(str string, wrapWith string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Wrap("foo", "") - result2 := strutil.Wrap("foo", "*") - result3 := strutil.Wrap("'foo'", "'") - result4 := strutil.Wrap("", "*") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // foo - // *foo* - // ''foo'' - // -} -``` - -### Wrap - -用另一个字符串解开包裹一个字符串。
- -函数签名: - -```go -func Unwrap(str string, wrapToken string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Unwrap("foo", "") - result2 := strutil.Unwrap("*foo*", "*") - result3 := strutil.Unwrap("*foo", "*") - result4 := strutil.Unwrap("foo*", "*") - result5 := strutil.Unwrap("**foo**", "*") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // foo - // foo - // *foo - // foo* - // *foo* -} -``` - -### SplitWords - -将字符串拆分为单词,只支持字母字符单词。
- -函数签名: - -```go -func SplitWords(s string) []string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.SplitWords("a word") - result2 := strutil.SplitWords("I'am a programmer") - result3 := strutil.SplitWords("Bonjour, je suis programmeur") - result4 := strutil.SplitWords("a -b-c' 'd'e") - result5 := strutil.SplitWords("你好,我是一名码农") - result6 := strutil.SplitWords("こんにちは,私はプログラマーです") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // [a word] - // [I'am a programmer] - // [Bonjour je suis programmeur] - // [a b-c' d'e] - // [] - // [] -} -``` - -### WordCount - -返回有意义单词的数量,只支持字母字符单词。
- -函数签名: - -```go -func WordCount(s string) int -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.WordCount("a word") - result2 := strutil.WordCount("I'am a programmer") - result3 := strutil.WordCount("Bonjour, je suis programmeur") - result4 := strutil.WordCount("a -b-c' 'd'e") - result5 := strutil.WordCount("你好,我是一名码农") - result6 := strutil.WordCount("こんにちは,私はプログラマーです") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // 2 - // 3 - // 4 - // 3 - // 0 - // 0 -} -``` - -### RemoveNonPrintable - -删除字符串中不可打印的字符。
- -函数签名: - -```go -func RemoveNonPrintable(str string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n") - result2 := strutil.RemoveNonPrintable("你好😄") - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // hello world - // 你好😄 -} -``` - -### StringToBytes - -在不分配内存的情况下将字符串转换为字节片。
- -函数签名: - -```go -func StringToBytes(str string) (b []byte) -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.StringToBytes("abc") - result2 := reflect.DeepEqual(result1, []byte{'a', 'b', 'c'}) - - fmt.Println(result1) - fmt.Println(result2) - // Output: - // [97 98 99] - // true -} -``` - -### BytesToString - -在不分配内存的情况下将字节切片转换为字符串。
- -函数签名: - -```go -func BytesToString(bytes []byte) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - bytes := []byte{'a', 'b', 'c'} - result := strutil.BytesToString(bytes) - - fmt.Println(result) - - // Output: - // abc -} -``` - -### IsBlank - -检查字符串是否为空格或空。
- -函数签名: - -```go -func IsBlank(str string) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.IsBlank("") - result2 := strutil.IsBlank("\t\v\f\n") - result3 := strutil.IsBlank(" 中文") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - - // Output: - // true - // true - // false -} -``` - -### HasPrefixAny - -检查字符串是否以指定字符串数组中的任何一个开头。
- -函数签名: - -```go -func HasPrefixAny(str string, prefixes []string) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"}) - result2 := strutil.HasPrefixAny("foo bar", []string{"oom", "world"}) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### HasSuffixAny - -检查字符串是否以指定字符串数组中的任何一个结尾。
- -函数签名: - -```go -func HasSuffixAny(str string, suffixes []string) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.HasSuffixAny("foo bar", []string{"bar", "xyz", "hello"}) - result2 := strutil.HasSuffixAny("foo bar", []string{"oom", "world"}) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### IndexOffset - -将字符串偏移idxFrom后,返回字符串中第一个 substr 实例的索引,如果字符串中不存在 substr,则返回 -1。
- -函数签名: - -```go -func IndexOffset(str string, substr string, idxFrom int) int -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "foo bar hello world" - - result1 := strutil.IndexOffset(str, "o", 5) - result2 := strutil.IndexOffset(str, "o", 0) - result3 := strutil.IndexOffset(str, "d", len(str)-1) - result4 := strutil.IndexOffset(str, "d", len(str)) - result5 := strutil.IndexOffset(str, "f", -1) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - - // Output: - // 12 - // 1 - // 18 - // -1 - // -1 -} -``` - -### ReplaceWithMap - -返回`str`的副本,以无序的方式被map替换,区分大小写。
- -函数签名: - -```go -func ReplaceWithMap(str string, replaces map[string]string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "ac ab ab ac" - replaces := map[string]string{ - "a": "1", - "b": "2", - } - - result := strutil.ReplaceWithMap(str, replaces) - - fmt.Println(result) - // Output: - // 1c 12 12 1c -} -``` - -### Trim - -从字符串的开头和结尾去除空格(或其他字符)。 可选参数 characterMask 指定额外的剥离字符。
- -函数签名: - -```go -func Trim(str string, characterMask ...string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - result1 := strutil.Trim("\nabcd") - - str := "$ ab cd $ " - - result2 := strutil.Trim(str) - result3 := strutil.Trim(str, "$") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - - // Output: - // abcd - // $ ab cd $ - // ab cd -} -``` - -### SplitAndTrim - -将字符串str按字符串delimiter拆分为一个切片,并对该数组的每个元素调用Trim。忽略Trim后为空的元素。
- -函数签名: - -```go -func SplitAndTrim(str, delimiter string, characterMask ...string) []string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := " a,b, c,d,$1 " - - result1 := strutil.SplitAndTrim(str, ",") - result2 := strutil.SplitAndTrim(str, ",", "$") - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // [a b c d $1] - // [a b c d 1] -} -``` - -### HideString - -使用参数`replaceChar`隐藏源字符串中的一些字符。替换范围是 origin[start : end]。
- -函数签名: - -```go -func HideString(origin string, start, end int, replaceChar string) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "13242658976" - - result1 := strutil.HideString(str, 3, 3, "*") - result2 := strutil.HideString(str, 3, 4, "*") - result3 := strutil.HideString(str, 3, 7, "*") - result4 := strutil.HideString(str, 7, 11, "*") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // 13242658976 - // 132*2658976 - // 132****8976 - // 1324265**** -} -``` - -### ContainsAll - -判断字符串是否包括全部给定的子字符串切片。
- -函数签名: - -```go -func ContainsAll(str string, substrs []string) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "hello world" - - result1 := strutil.ContainsAll(str, []string{"hello", "world"}) - result2 := strutil.ContainsAll(str, []string{"hello", "abc"}) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // true - // false -} -``` - -### ContainsAny - -判断字符串是否包括给定的子字符串切片中任意一个子字符串。
- -函数签名: - -```go -func ContainsAny(str string, substrs []string) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := "hello world" - - result1 := strutil.ContainsAny(str, []string{"hello", "world"}) - result2 := strutil.ContainsAny(str, []string{"hello", "abc"}) - result3 := strutil.ContainsAny(str, []string{"123", "abc"}) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - - // Output: - // true - // true - // false -} -``` - -### RemoveWhiteSpace - -删除字符串中的空格,当设置repalceAll为true时,删除全部空格,为false时,替换多个空格为1个空格。
- -函数签名: - -```go -func RemoveWhiteSpace(str string, repalceAll bool) string -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - str := " hello \r\n \t world" - - result1 := strutil.RemoveWhiteSpace(str, true) - result2 := strutil.RemoveWhiteSpace(str, false) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // helloworld - // hello world -} -``` diff --git a/docs/olddocs/system.md b/docs/olddocs/system.md deleted file mode 100644 index 84b6f02..0000000 --- a/docs/olddocs/system.md +++ /dev/null @@ -1,307 +0,0 @@ -# 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: - -```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: - -```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: - -```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: - -```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: - //Sets the value of the environment variable named by the key.
- -Signature: - -```go -func SetOsEnv(key, value string) error -``` - -Example: - -```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: - //Remove a single environment variable.
- -Signature: - -```go -func RemoveOsEnv(key string) error -``` - -Example: - -```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: - //Get env named by the key and compare it with comparedEnv.
- -Signature: - -```go -func CompareOsEnv(key, comparedEnv string) bool -``` - -Example: - -```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: - -```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: - -```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/olddocs/system_zh-CN.md b/docs/olddocs/system_zh-CN.md deleted file mode 100644 index e165e38..0000000 --- a/docs/olddocs/system_zh-CN.md +++ /dev/null @@ -1,307 +0,0 @@ -# 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 -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/system" -) - -func main() { - isOsWindows := system.IsWindows() - fmt.Println(isOsWindows) -} -``` - -### IsLinux - -检查当前操作系统是否是linux
- -函数签名: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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: - //设置由key命名的环境变量的值
- -函数签名: - -```go -func SetOsEnv(key, value string) error -``` - -示例: - -```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: - //删除单个环境变量
- -函数签名: - -```go -func RemoveOsEnv(key string) error -``` - -示例: - -```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: - //获取key命名的环境变量值并与compareEnv进行比较
- -函数签名: - -```go -func CompareOsEnv(key, comparedEnv string) bool -``` - -示例: - -```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) -``` - -示例: - -```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 -``` - -示例: - -```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/olddocs/tuple.md b/docs/olddocs/tuple.md deleted file mode 100644 index 182f109..0000000 --- a/docs/olddocs/tuple.md +++ /dev/null @@ -1,1197 +0,0 @@ -# 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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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/olddocs/tuple_zh-CN.md b/docs/olddocs/tuple_zh-CN.md deleted file mode 100644 index 85266fc..0000000 --- a/docs/olddocs/tuple_zh-CN.md +++ /dev/null @@ -1,1197 +0,0 @@ -# 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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] - -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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] - -``` - -示例: - -```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) -``` - -示例: - -```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] -``` - -示例: - -```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) -``` - -示例: - -```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/olddocs/validator.md b/docs/olddocs/validator.md deleted file mode 100644 index 98aeb5b..0000000 --- a/docs/olddocs/validator.md +++ /dev/null @@ -1,1188 +0,0 @@ -# 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/olddocs/validator_zh-CN.md b/docs/olddocs/validator_zh-CN.md deleted file mode 100644 index 7280311..0000000 --- a/docs/olddocs/validator_zh-CN.md +++ /dev/null @@ -1,1192 +0,0 @@ -# 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/olddocs/xerror.md b/docs/olddocs/xerror.md deleted file mode 100644 index c185403..0000000 --- a/docs/olddocs/xerror.md +++ /dev/null @@ -1,488 +0,0 @@ -# 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: - -```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: - -```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: - -```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: - -```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 -} -``` - -### XError_Unwrap - -Compatible with github.com/pkg/errors.
- -Signature: - -```go -func (e *XError) Unwrap() error -``` - -Example: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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: - -```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/olddocs/xerror_zh-CN.md b/docs/olddocs/xerror_zh-CN.md deleted file mode 100644 index f3c23bf..0000000 --- a/docs/olddocs/xerror_zh-CN.md +++ /dev/null @@ -1,489 +0,0 @@ -# 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 -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -} -``` - -### XError_Unwrap - -解构XEerror为error对象。适配github.com/pkg/errors。
- -函数签名: - -```go -func (e *XError) Unwrap() error -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -``` - -示例: - -```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 -} -```