mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 06:02:27 +08:00
doc: update document for version v2.3.8
This commit is contained in:
@@ -33,6 +33,9 @@ import (
|
||||
- [ToJson](#ToJson)
|
||||
- [ToMap](#ToMap)
|
||||
- [ToPointer](#ToPointer)
|
||||
- [ToPointers](#ToPointers)
|
||||
- [FromPointer](#FromPointer)
|
||||
- [FromPointers](#FromPointers)
|
||||
- [ToString](#ToString)
|
||||
- [StructToMap](#StructToMap)
|
||||
- [MapToSlice](#MapToSlice)
|
||||
@@ -456,6 +459,108 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="ToPointers">ToPointers</span>
|
||||
|
||||
<p>将值的切片转换为指针的切片。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ToPointers[T any](values []T) []*T
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ZUoXd2i5ZkV)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/convertor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
strs := []string{"a", "b", "c"}
|
||||
pointerStrs := convertor.ToPointers(strs)
|
||||
fmt.Println(*pointerStrs[0])
|
||||
fmt.Println(*pointerStrs[1])
|
||||
fmt.Println(*pointerStrs[2])
|
||||
|
||||
// Output:
|
||||
// a
|
||||
// b
|
||||
// c
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="FromPointer">FromPointer</span>
|
||||
|
||||
<p>返回指针所指向的值。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func FromPointer[T any](ptr *T) T
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/wAp90V7Zu6g)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/convertor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
str := "abc"
|
||||
strPtr := &str
|
||||
result := convertor.FromPointer(strPtr)
|
||||
fmt.Println(result)
|
||||
|
||||
// Output:
|
||||
// abc
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="FromPointers">FromPointers</span>
|
||||
|
||||
<p>将指针的切片转换为值的切片。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func FromPointers[T any](pointers []*T) []T
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/qIPsyYtNy3Q)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/convertor"
|
||||
)
|
||||
|
||||
func main() {
|
||||
strs := []string{"a", "b", "c"}
|
||||
strPtr := []*string{&strs[0], &strs[1], &strs[2]}
|
||||
|
||||
result := convertor.FromPointers(strPtr)
|
||||
|
||||
fmt.Println(result[0])
|
||||
fmt.Println(result[1])
|
||||
fmt.Println(result[2])
|
||||
|
||||
// Output:
|
||||
// a
|
||||
// b
|
||||
// c
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="ToString">ToString</span>
|
||||
|
||||
<p>将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构体)将调用 json.Marshal</p>
|
||||
@@ -1179,4 +1284,4 @@ func main() {
|
||||
// Output:
|
||||
// 9876543210
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# Enum
|
||||
|
||||
Enum实现一个简单枚举工具包。
|
||||
Enum 实现一个简单枚举工具包。
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 源码:
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/enum/enum.go](https://github.com/duke-git/lancet/blob/main/enum/enum.go)
|
||||
- [https://github.com/duke-git/lancet/blob/main/enum/enum.go](https://github.com/duke-git/lancet/blob/main/enum/enum.go)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -55,7 +55,7 @@ import (
|
||||
func NewItem[T comparable](value T, name string) *Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/8qNsLw01HD5)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -96,7 +96,7 @@ func main() {
|
||||
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -139,7 +139,7 @@ func main() {
|
||||
func (it *Item[T]) Value() T
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -182,7 +182,7 @@ func main() {
|
||||
func (it *Item[T]) Name() string
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -225,7 +225,7 @@ func main() {
|
||||
func (it *Item[T]) Valid(checker ...func(T) bool) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/pA3lYY2VSm3)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -258,7 +258,7 @@ func main() {
|
||||
|
||||
### <span id="MarshalJSON">MarshalJSON</span>
|
||||
|
||||
<p>枚举项实现json.Marshaler 接口。</p>
|
||||
<p>枚举项实现json.Marshaler接口。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
@@ -267,7 +267,7 @@ func (it *Item[T]) MarshalJSON() ([]byte, error)
|
||||
func (it *Item[T]) UnmarshalJSON(data []byte) error
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/zIZEdAnneB5)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -290,7 +290,7 @@ func main() {
|
||||
data, _ := item.MarshalJSON()
|
||||
fmt.Println(string(data))
|
||||
|
||||
var unmarshaledItem Item[Status]
|
||||
var unmarshaledItem enum.Item[Status]
|
||||
_ = unmarshaledItem.UnmarshalJSON(data)
|
||||
fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
|
||||
|
||||
@@ -310,7 +310,7 @@ func main() {
|
||||
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ABEXsYfJKMo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -359,7 +359,7 @@ func main() {
|
||||
func (r *Registry[T]) Add(items ...*Item[T])
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ABEXsYfJKMo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -408,7 +408,7 @@ func main() {
|
||||
func (r *Registry[T]) Remove(value T) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/dSG84wQ3TuC)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -454,7 +454,7 @@ func main() {
|
||||
func (r *Registry[T]) Update(value T, newName string) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/Ol0moT1J9Xl)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -500,7 +500,7 @@ func main() {
|
||||
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/niJ1U2KlE_m)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -520,9 +520,9 @@ const (
|
||||
|
||||
func main() {
|
||||
registry := enum.NewRegistry[Status]()
|
||||
item1 := enum.NewItem(Active, "Active")
|
||||
item := enum.NewItem(Active, "Active")
|
||||
|
||||
registry.Add(item1, item2)
|
||||
registry.Add(item)
|
||||
|
||||
if item, found := registry.GetByValue(Active); found {
|
||||
fmt.Println("Found name by value:", item.Name())
|
||||
@@ -543,7 +543,7 @@ func main() {
|
||||
func (r *Registry[T]) GetByName(name string) (*Item[T], bool)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/49ie_gpqH0m)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -586,7 +586,7 @@ func main() {
|
||||
func (r *Registry[T]) Items() []*Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/lAJFAradbvQ)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -631,7 +631,7 @@ func main() {
|
||||
func (r *Registry[T]) Contains(value T) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/_T-lPYkZn2j)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -673,7 +673,7 @@ func main() {
|
||||
func (r *Registry[T]) Size() int
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/TeDArWhlQe2)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -721,7 +721,7 @@ func main() {
|
||||
func (r *Registry[T]) Range(fn func(*Item[T]) bool)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/GPsZbQbefWN)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -745,7 +745,7 @@ func main() {
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
registry.Range(func(item *Item[Status]) bool {
|
||||
registry.Range(func(item *enum.Item[Status]) bool {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
return true // continue iteration
|
||||
})
|
||||
@@ -766,7 +766,7 @@ func main() {
|
||||
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/tN9RE_m_WEI)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -790,8 +790,8 @@ func main() {
|
||||
item2 := enum.NewItem(Active, "Active")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
for _, item := range registry.SortedItems(func(i1, i2 *Item[Status]) bool {
|
||||
return i1.value < i2.value
|
||||
for _, item := range registry.SortedItems(func(i1, i2 *enum.Item[Status]) bool {
|
||||
return i1.Value() < i2.Value()
|
||||
}) {
|
||||
fmt.Println(item.Name(), item.Value())
|
||||
}
|
||||
@@ -812,7 +812,7 @@ func main() {
|
||||
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/uTUpTdcyoCU)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -836,7 +836,7 @@ func main() {
|
||||
item2 := enum.NewItem(Inactive, "Inactive")
|
||||
registry.Add(item1, item2)
|
||||
|
||||
activeItems := registry.Filter(func(item *Item[Status]) bool {
|
||||
activeItems := registry.Filter(func(item *enum.Item[Status]) bool {
|
||||
return item.Value() == Active
|
||||
})
|
||||
|
||||
@@ -847,4 +847,4 @@ func main() {
|
||||
// Output:
|
||||
// Active 1
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -2357,8 +2357,8 @@ func main() {
|
||||
编辑
|
||||
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
|
||||
```
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/w_pSLfeyEB5)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -2410,4 +2410,4 @@ func main() {
|
||||
// |50000|Alice|
|
||||
// |60000|Bob|
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -69,7 +69,7 @@ func main() {
|
||||
}
|
||||
p1 := &People{Name: "11"}
|
||||
s := structs.New(p1)
|
||||
|
||||
|
||||
fmt.Println(s.ToMap())
|
||||
|
||||
// Output:
|
||||
@@ -261,7 +261,7 @@ func main() {
|
||||
tag := n.Tag()
|
||||
|
||||
fmt.Println(tag.Name)
|
||||
|
||||
|
||||
// Output:
|
||||
// name
|
||||
}
|
||||
@@ -295,10 +295,10 @@ func main() {
|
||||
|
||||
s := structs.New(p1)
|
||||
n, _ := s.Field("Name")
|
||||
|
||||
|
||||
fmt.Println(n.Value())
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// 111
|
||||
}
|
||||
```
|
||||
@@ -338,11 +338,11 @@ func main() {
|
||||
s := structs.New(c1)
|
||||
n, _ := s.Field("Name")
|
||||
a, _ := s.Field("Age")
|
||||
|
||||
|
||||
fmt.Println(n.IsEmbedded())
|
||||
fmt.Println(a.IsEmbedded())
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
@@ -377,11 +377,11 @@ func main() {
|
||||
s := structs.New(p1)
|
||||
n, _ := s.Field("Name")
|
||||
a, _ := s.Field("age")
|
||||
|
||||
|
||||
fmt.Println(n.IsExported())
|
||||
fmt.Println(a.IsExported())
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
@@ -416,11 +416,11 @@ func main() {
|
||||
s := structs.New(p1)
|
||||
n, _ := s.Field("Name")
|
||||
a, _ := s.Field("Age")
|
||||
|
||||
|
||||
fmt.Println(n.IsZero())
|
||||
fmt.Println(a.IsZero())
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
}
|
||||
@@ -455,11 +455,11 @@ func main() {
|
||||
s := structs.New(p1)
|
||||
n, _ := s.Field("Name")
|
||||
a, _ := s.Field("Age")
|
||||
|
||||
|
||||
fmt.Println(n.Name())
|
||||
fmt.Println(a.Name())
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// Name
|
||||
// Age
|
||||
}
|
||||
@@ -494,11 +494,11 @@ func main() {
|
||||
s := structs.New(p1)
|
||||
n, _ := s.Field("Name")
|
||||
a, _ := s.Field("Age")
|
||||
|
||||
|
||||
fmt.Println(n.Kind())
|
||||
fmt.Println(a.Kind())
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// string
|
||||
// int
|
||||
}
|
||||
@@ -514,7 +514,7 @@ func main() {
|
||||
func (s *Struct) TypeName() string
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/SWLWd0XBaBb)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -529,13 +529,13 @@ func main() {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
|
||||
p := &Parent{Age: 11}
|
||||
s := structs.New(p1)
|
||||
s := structs.New(p)
|
||||
|
||||
fmt.Println(s.TypeName())
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// Parent
|
||||
}
|
||||
```
|
||||
@@ -569,10 +569,10 @@ func main() {
|
||||
p1 := &Parent{arr: []int{1, 2, 3}}
|
||||
s := structs.New(p1)
|
||||
a, _ := s.Field("arr")
|
||||
|
||||
|
||||
fmt.Println(a.IsSlice())
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
```
|
||||
@@ -608,12 +608,12 @@ func main() {
|
||||
s := structs.New(p1)
|
||||
n, _ := s.Field("Name")
|
||||
a, _ := s.Field("arr")
|
||||
|
||||
|
||||
fmt.Println(n.IsTargetType(reflect.String))
|
||||
fmt.Println(a.IsTargetType(reflect.Slice))
|
||||
|
||||
// Output:
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1552,6 +1552,8 @@ func IsChinaUnionPay(v string) bool
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/yafpdxLiymu)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/validator"
|
||||
@@ -1580,9 +1582,11 @@ func main() {
|
||||
func IsPassport(passport, country string) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/dvOiV2BW7Aw)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/validator"
|
||||
@@ -1617,7 +1621,7 @@ func main() {
|
||||
func IsChineseHMPassport(hmPassport string) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/xKG6spQTcY0)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -1645,4 +1649,4 @@ func main() {
|
||||
// false
|
||||
// false
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user