mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-12 08:42:29 +08:00
doc: update slice document
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
- [AppendIfAbsent](#AppendIfAbsent)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
- [ContainSubSlice](#ContainSubSlice)
|
- [ContainSubSlice](#ContainSubSlice)
|
||||||
- [Chunk](#Chunk)
|
- [Chunk](#Chunk)
|
||||||
@@ -53,6 +54,8 @@ import (
|
|||||||
- [SortByField](#SortByField)
|
- [SortByField](#SortByField)
|
||||||
- [Some](#Some)
|
- [Some](#Some)
|
||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
|
- [ToSlice](#ToSlice)
|
||||||
|
- [ToSlicePointer](#ToSlicePointer)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [UniqueBy](#UniqueBy)
|
- [UniqueBy](#UniqueBy)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
@@ -66,6 +69,35 @@ import (
|
|||||||
## Note:
|
## Note:
|
||||||
1. param which type is interface{} in below functions should be slice.
|
1. param which type is interface{} in below functions should be slice.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
||||||
|
<p>If slice doesn't contain the value, append it to the slice.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AppendIfAbsent[T comparable](slice []T, value T) []T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strs := []string{"a", "b"}
|
||||||
|
res1 := slice.AppendIfAbsent(strs, "a")
|
||||||
|
fmt.Println(res1) //[]string{"a", "b"}
|
||||||
|
|
||||||
|
res2 := slice.AppendIfAbsent(strs, "cannot")
|
||||||
|
fmt.Println(res2"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Contain">Contain</span>
|
### <span id="Contain">Contain</span>
|
||||||
<p>Check if the value is in the slice or not. iterableType param can be string, map or slice.</p>
|
<p>Check if the value is in the slice or not. iterableType param can be string, map or slice.</p>
|
||||||
|
|
||||||
@@ -974,6 +1006,55 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToSlice">ToSlice</span>
|
||||||
|
<p>Returns a slices of a variable parameter transformation</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToSlice[T any](value ...T) []T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
res := slice.ToSlice("a", "b")
|
||||||
|
fmt.Println(res) //{"a", "b"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||||
|
<p>Returns a pointer to the slices of a variable parameter transformation</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToSlicePointer[T any](value ...T) []*T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str1 := "a"
|
||||||
|
str2 := "b"
|
||||||
|
res := slice.ToSlicePointer(str1, str2)
|
||||||
|
fmt.Println(res) // res -> []*string{&str1, &str2}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
<p>Remove duplicate elements in slice.</p>
|
<p>Remove duplicate elements in slice.</p>
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
- [AppendIfAbsent](#AppendIfAbsent)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
- [ContainSubSlice](#ContainSubSlice)
|
- [ContainSubSlice](#ContainSubSlice)
|
||||||
- [Chunk](#Chunk)
|
- [Chunk](#Chunk)
|
||||||
@@ -53,6 +54,8 @@ import (
|
|||||||
- [SortByField](#SortByField)
|
- [SortByField](#SortByField)
|
||||||
- [Some](#Some)
|
- [Some](#Some)
|
||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
|
- [ToSlice](#ToSlice)
|
||||||
|
- [ToSlicePointer](#ToSlicePointer)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [UniqueBy](#UniqueBy)
|
- [UniqueBy](#UniqueBy)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
@@ -63,6 +66,34 @@ import (
|
|||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
||||||
|
<p>当前切片中不包含值时,将该值追加到切片中</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AppendIfAbsent[T comparable](slice []T, value T) []T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strs := []string{"a", "b"}
|
||||||
|
res1 := slice.AppendIfAbsent(strs, "a")
|
||||||
|
fmt.Println(res1) //[]string{"a", "b"}
|
||||||
|
|
||||||
|
res2 := slice.AppendIfAbsent(strs, "cannot")
|
||||||
|
fmt.Println(res2"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Contain">Contain</span>
|
### <span id="Contain">Contain</span>
|
||||||
<p>判断slice是否包含value</p>
|
<p>判断slice是否包含value</p>
|
||||||
|
|
||||||
@@ -974,6 +1005,55 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToSlice">ToSlice</span>
|
||||||
|
<p>将可变参数转为切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToSlice[T any](value ...T) []T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
res := slice.ToSlice("a", "b")
|
||||||
|
fmt.Println(res) //{"a", "b"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||||
|
<p>将可变参数转为指针切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToSlicePointer[T any](value ...T) []*T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str1 := "a"
|
||||||
|
str2 := "b"
|
||||||
|
res := slice.ToSlicePointer(str1, str2)
|
||||||
|
fmt.Println(res) // res -> []*string{&str1, &str2}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
<p>删除切片中的重复元素</p>
|
<p>删除切片中的重复元素</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user