mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
doc: add doc for UniqueByField
This commit is contained in:
@@ -86,6 +86,7 @@ import (
|
|||||||
- [ToSlicePointer](#ToSlicePointer)
|
- [ToSlicePointer](#ToSlicePointer)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [UniqueBy](#UniqueBy)
|
- [UniqueBy](#UniqueBy)
|
||||||
|
- [UniqueByField](#UniqueByField)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [UnionBy](#UnionBy)
|
- [UnionBy](#UnionBy)
|
||||||
- [UpdateAt](#UpdateAt)
|
- [UpdateAt](#UpdateAt)
|
||||||
@@ -2312,6 +2313,47 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UniqueByField">UniqueByField</span>
|
||||||
|
|
||||||
|
<p>根据struct字段对struct切片去重复。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UniqueByField[T any](slice []T, field string) ([]T, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;"></span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type User struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
users := []User{
|
||||||
|
{ID: 1, Name: "a"},
|
||||||
|
{ID: 2, Name: "b"},
|
||||||
|
{ID: 1, Name: "c"},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := slice.UniqueByField(users, "ID")
|
||||||
|
if err != nil {
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [{1 a} {2 b}]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Union">Union</span>
|
### <span id="Union">Union</span>
|
||||||
|
|
||||||
<p>合并多个切片</p>
|
<p>合并多个切片</p>
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ import (
|
|||||||
- [ToSlicePointer](#ToSlicePointer)
|
- [ToSlicePointer](#ToSlicePointer)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [UniqueBy](#UniqueBy)
|
- [UniqueBy](#UniqueBy)
|
||||||
|
- [UniqueByField](#UniqueByField)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [UnionBy](#UnionBy)
|
- [UnionBy](#UnionBy)
|
||||||
- [UpdateAt](#UpdateAt)
|
- [UpdateAt](#UpdateAt)
|
||||||
@@ -2310,6 +2311,47 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="UniqueByField">UniqueByField</span>
|
||||||
|
|
||||||
|
<p>Remove duplicate elements in struct slice by struct field.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UniqueByField[T any](slice []T, field string) ([]T, error)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;"></span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type User struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
users := []User{
|
||||||
|
{ID: 1, Name: "a"},
|
||||||
|
{ID: 2, Name: "b"},
|
||||||
|
{ID: 1, Name: "c"},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := slice.UniqueByField(users, "ID")
|
||||||
|
if err != nil {
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [{1 a} {2 b}]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Union">Union</span>
|
### <span id="Union">Union</span>
|
||||||
|
|
||||||
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user