Skip to content

Commit

Permalink
doc: add doc for UniqueByField
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Jun 25, 2024
1 parent 95b516e commit 2097277
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 16 deletions.
74 changes: 58 additions & 16 deletions docs/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import (
- [ToSlicePointer](#ToSlicePointer)
- [Unique](#Unique)
- [UniqueBy](#UniqueBy)
- [UniqueByField](#UniqueByField)
- [Union](#Union)
- [UnionBy](#UnionBy)
- [UpdateAt](#UpdateAt)
Expand Down Expand Up @@ -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>

<p>合并多个切片</p>
Expand Down Expand Up @@ -2594,14 +2636,14 @@ import (

func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s })
modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s })
fmt.Println(modifiedStrs)
fmt.Println(count)
fmt.Println(count)
// Output:
// [ b c d ]
// 3
// [ b c d ]
// 3
}
```

Expand Down Expand Up @@ -2657,11 +2699,11 @@ import (
)

func main() {
nums := []int{1, 2, 3, 4, 5}
padded := slice.RightPadding(nums, 0, 3)
fmt.Println(padded)
// Output:
// [1 2 3 4 5 0 0 0]
nums := []int{1, 2, 3, 4, 5}
padded := slice.RightPadding(nums, 0, 3)
fmt.Println(padded)
// Output:
// [1 2 3 4 5 0 0 0]
}
```

Expand All @@ -2684,10 +2726,10 @@ import (
)

func main() {
nums := []int{1, 2, 3, 4, 5}
padded := slice.LeftPadding(nums, 0, 3)
fmt.Println(padded)
// Output:
// [0 0 0 1 2 3 4 5]
nums := []int{1, 2, 3, 4, 5}
padded := slice.LeftPadding(nums, 0, 3)
fmt.Println(padded)
// Output:
// [0 0 0 1 2 3 4 5]
}
```
42 changes: 42 additions & 0 deletions docs/en/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import (
- [ToSlicePointer](#ToSlicePointer)
- [Unique](#Unique)
- [UniqueBy](#UniqueBy)
- [UniqueByField](#UniqueByField)
- [Union](#Union)
- [UnionBy](#UnionBy)
- [UpdateAt](#UpdateAt)
Expand Down Expand Up @@ -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>

<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
Expand Down

0 comments on commit 2097277

Please sign in to comment.